惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Vercel News
Vercel News
博客园_首页
Y
Y Combinator Blog
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
博客园 - Franky
Engineering at Meta
Engineering at Meta
量子位
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium

博客园 - Anytao

[视频],花一分钟来看看Worktile是如何为团队协作而生的 如何使用Worktile进行敏捷项目开发管理 有一个团队协同工具,叫Worktile 上博客园,用工作台 [WP7], #01, Windows Phone 7培训点滴 [推荐], 开始Azure之旅,参加深度培训 [推荐],与创业公司一起成长 [推荐], 游戏?地图?还是交友?尽在16fun 2010,因为不同:挺进2010,记忆2009 [你必须知道的.NET]第三十五回,判断dll是debug还是release,这是个问题 [关注]日益火爆的Social media - Anytao - 博客园 [开发故事]第五回,用想要的域名运行你的本地Web应用 [你必须知道的.NET]第三十四回,object成员,不见了! [你必须知道的.NET]第三十三回,深入.NET 4.0之,Lazy<T>点滴 [活动]博文视点Open Party上海站职业生涯分论坛 哥写的不是代码,是寂寞 [一些思考]转身,关注更高。 [书评杂谈]观止,观而不止 [书评杂谈]《我也能做CTO》 书评
[开发故事],第十五回,在Js中应用命名空间
Anytao · 2010-10-22 · via 博客园 - Anytao

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.