[开发故事],第十五回,在Js中应用命名空间

anytao.net | 《你必须知道的.NET》网站 | Anytao技术博客 

发布日期:2010.10.22 作者:Anytao
© 2010 Anytao.com ,Anytao原创作品,转贴请注明作者和出处。

Introduction

How to have a better code organization? When your software become bigger and bigger, the code will torture you all the time. So, the smart guy  innovate the Namespace to handle this issue. For example, in .NET world, we define the class in scope of namespace:

namespace Anytao.Common
{
    public class Console
    {
        public static void Read(string msg)
        { 
        }
    }
}

Then, we can use Console as below:

Anytao.Common.Console.Read("Hello, world.");

It will bring the following benefit:

  • Better code organization
  • Avoid naming conflict. For example, If we have another Console in the same assembly, it will cause error without namespace control. Now, we can absolutely separate naming by namespace.
Anytao.Common.Console.Read("Hello, world.");

System.Console.Read();

Namespace in JavaScript

However, there is no language level support in JavaScript. It will cause a lot of problem and mess the process of development. Here is a experience in our project.

/* Define the NS in imxiqi.js */
var X8JS = {};
X8JS.ns = function (path) {
    var arr = path.split(".");
    var ns = "";
    for (var i = 0; i < arr.length; i++) {
        if (i > 0)
            ns += ".";
        ns += arr[i];
        eval("if(typeof(" + ns + ") == 'undefined') " + ns + " = new Object();");
    }
};

(注,上述代码来自互联网)

How to use?

  • Reference the imxiqi.js file in your page head.

<scriptsrc="http://www.cnblogs.com/static/js/imxiqi.js"type="text/javascript"></script>

  • Register the namespace when you use

X8JS.ns("XLR8.feed");

  • Define the class, variables and others in your namespace

X8JS.ns("XLR8.feed");
X8JS.ns("Ethos.common");

XLR8.feed =
{
    alert: function (msg) {
        alert(msg);
    },

    load: function () {
    }

};

Ethos.common.copyright = function BindData(data) {
    $("#copyright").html( data + " / Ethos");
};

  • Use it when you use
    <script language="javascript" type="text/javascript">

        $(document).ready(function () {

            XLR8.feed.alert("Hello");

            XLR8.feed.load();

            Ethos.common.copyright("Anytao");
        })

    </script>

Hey. It’s simple and useful.

 

 

更多闲言碎语,关注anytao.net

 

anytao | © 2010 Anytao.com

2010/10/22 | http://anytao.cnblogs.com/ | http://anytao.net/blog/post/2010/10/22/anytao-devstory-15-use-namespace-in-js.aspx

本文以“现状”提供且没有任何担保,同时也没有授予任何权利。 | This posting is provided "AS IS" with no warranties, and confers no rights.

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @   Anytao  阅读(3509)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2007-10-22 [CLR团队]精品系列第一期:不止是起点
点击右上角即可分享
微信分享提示