












anytao.net | 《你必须知道的.NET》网站 | Anytao技术博客
发布日期:2010.10.22 作者:Anytao
© 2010 Anytao.com ,Anytao原创作品,转贴请注明作者和出处。
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:
Anytao.Common.Console.Read("Hello, world."); System.Console.Read();
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();"); } };
(注,上述代码来自互联网)
<scriptsrc="http://www.cnblogs.com/static/js/imxiqi.js"type="text/javascript"></script>
X8JS.ns("XLR8.feed");
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"); };
<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.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。