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

推荐订阅源

MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
美团技术团队
B
Blog
量子位
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
aimingoo的专栏
aimingoo的专栏
The GitHub Blog
The GitHub Blog
博客园 - 聂微东
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题

博客园 - Jacken

ios basics 关于 php mvc Simple JavaScript Inheritance javascript 命名空间 继承 实现 转: CSS网页布局教程:绝对定位和相对定位 js 类与对象 div 垂直居中 布局 boost signal 用法与用处... Flash嵌入纯Win32程序 及 事件接收 消息发送器设计 工若善其器,必然利其事。 HTML5开发工具选择 项目 "差不多成功? 简直就是失败". html css 布局 ubuntu 使用 wifi 连接上网 用背景图片填充Edit控件... 游戏类初步一.. C++的异常处理方法之一. - Jacken - 博客园 C++ EventHandler v0.02 在命令行中使用cl工具生成纯资源的DLL文件...
转: Basic JavaScript Part 8: Namespaces
Jacken · 2012-05-16 · via 博客园 - Jacken

源链接: http://elegantcode.com/2011/01/26/basic-javascript-part-8-namespaces/ 
 

Here are the links to the previous installments:

  1. Functions
  2. Objects
  3. Prototypes
  4. Enforcing New on Constructor Functions
  5. Hoisting
  6. Automatic Semicolon Insertion
  7. Static Properties and Methods

In my previous post, I showed how you can ‘emulate’ static members in JavaScript without having a dedicated syntax for it. For this post, I’m going to discuss namespaces in JavaScript. In order to reduce the number of objects and functions that are added to the global scope in our applications, using namespaces in JavaScript is definitely a recommended practice. Just like static members, namespaces don’t have any dedicated syntax built into the language either. But we’re able to get the same benefits by creating a single global object and adding all our objects and functions to this object.

var AppSpace = AppSpace || {};  AppSpace.Podcast = function {     this.title = 'Astronomy Cast';     this.description = 'A fact-based journey through the galaxy.';     this.link = 'http://www.astronomycast.com'; };  AppSpace.Podcast.prototype.toString = function() {     return 'Title: ' + this.title; }

This way we lower the possibility of naming collisions when using our code in conjunction with other JavaScript libraries. We can also use the same naming conventions for namespaces as in any other programming language that does provide syntactical support. Suppose we want to use a namespace like “MyCompany.MyApplication.Model”. We can accomplish this by using the same approach as shown earlier:

var MyCompany = MyCompany || {}; MyCompany.MyApplication = {}; MyCompany.MyApplication.Model = {};

or something like the following:

var MyCompany = MyCompany || {     MyApplication: {         Model: {}     } };

However, this approach can become very cumbersome and hard to maintain when our code expands over time. In order to overcome this issue we can use a general purpose namespace function to achieve the same thing using a single line of code.

var model = namespace('MyCompany.MyApplication.Model');  model.Podcast = function {     this.title = 'Astronomy Cast';     this.description = 'A fact-based journey through the galaxy.';     this.link = 'http://www.astronomycast.com'; };  model.Podcast.prototype.toString = function() {     return 'Title: ' + this.title; }

Here’s an example of how we can implement such a namespace function.

function namespace(namespaceString) {     var parts = namespaceString.split('.'),         parent = window,         currentPart = '';          for(var i = 0, length = parts.length; i < length; i++) {         currentPart = parts[i];         parent[currentPart] = parent[currentPart] || {};         parent = parent[currentPart];     }      return parent; }

Using this approach requires less typing and the resulting code also looks less verbose. Some JavaScript libraries, like YUI and the Dojo Toolkit, provide their own implementations for such a namespace utility function. I encourage you to take a look at their implementations as well.

Until next time.