JavaScript Patterns 5.1 Namespace Pattern
global namespace object
// global object var MYAPP = {}; // constructors MYAPP.Parent = function() {
}; MYAPP.Child = function() {
}; // a variable MYAPP.some_var = 1; // an object container MYAPP.modules = {}; // nested objects MYAPP.modules.module1 = {}; MYAPP.modules.module1.data = {
a : 1,
b : 2
}; MYAPP.modules.module2 = {};
Drawbacks
• A bit more to type; prefixing every variable and function does add up in the total amount of code that needs to be downloaded
• Only one global instance means that any part of the code can modify the global instance and the rest of the functionality gets the updated state
• Long nested names mean longer (slower) property resolution lookups
General Purpose Namespace Function
// unsafe var MYAPP = {}; // better if ( typeof MYAPP === "undefined") { var MYAPP = {}; } // or shorter var MYAPP = MYAPP || {}; // using a namespace function MYAPP.namespace('MYAPP.modules.module2'); // equivalent to: // var MYAPP = { // modules: { // module2: {} // } // }; MYAPP.namespace = function(ns_string) { var parts = ns_string.split('.'), parent = MYAPP, i; // strip redundant leading global if (parts[0] === "MYAPP") { parts = parts.slice(1); } for ( i = 0; i < parts.length; i += 1) { // create a property if it doesn't exist if ( typeof parent[parts[i]] === "undefined") { parent[parts[i]] = {}; } parent = parent[parts[i]]; } return parent; }; // assign returned value to a local var var module2 = MYAPP.namespace('MYAPP.modules.module2'); module2 === MYAPP.modules.module2;
// true // skip initial `MYAPP` MYAPP.namespace('modules.module51'); // long namespace MYAPP.namespace('once.upon.a.time.there.was.this.long.nested.property');
References:
JavaScript Patterns - by Stoyan Stefanov (O`Reilly)
JavaScript Patterns 5.1 Namespace Pattern的更多相关文章
- JavaScript Patterns 5.5 Sandbox Pattern
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...
- JavaScript Patterns 5.4 Module Pattern
MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...
- JavaScript Patterns 5.8 Chaining Pattern
Chaining Pattern - Call methods on an object one after the other without assigning the return values ...
- JavaScript Patterns 4.2 Callback Pattern
function writeCode(callback) { // do something... callback(); // ... } function introduceBugs() { // ...
- JavaScript Patterns 2.6 switch Pattern
Principle • Aligning each case with switch(an exception to the curly braces indentation rule). • Ind ...
- JavaScript Patterns 4.8 Function Properties - A Memoization Pattern
Gets a length property containing the number of arguments the function expects: function func(a, b, ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
随机推荐
- java调用Oracle分页存储过程
Java程序 package com.test; import java.sql.CallableStatement; import java.sql.Connection; import java. ...
- 硬链接 and 软链接
硬链接 软链接
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- Output data in a cursor
http://www.java2s.com/Code/SQL/Cursor/Outputdatainacursor.htm mysql> mysql> mysql> CREATE T ...
- PHP程序员7小时学会Kotlin 第二小时
Kotlin中,一切皆对象:PHP则并非一切皆对象,甚至不需要对象的存在即可完成系统功能开发,我们现在可以接触到的旧的系统都可以说明这一点. 基本数据类型 数值型 类型 位长 双精度浮点型Double ...
- 双系统下删除Linux系统方法和Windows无法启动问题的解决方法
装了一个linux,后面直接把它删掉了,结果电脑重启的时候重启不了,总是一开机就出现 grub> 心中无比的恼火,后面想不通了,就打算直接重装系统,结果重装系统的过程中遇到了问题,B ...
- VS2010下安装Cocos2dx完整教程(原)
一.本人所使用的Cocos2dx版本(cocos2d-2.1rc0-x-2.1.3),下载地址:http://code.google.com/p/cocos2d-x/downloads/list 当前 ...
- 解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)
http://www.jb51.net/article/27616.htm 有次同事提出开发使用的mysql数据库连接很慢,因为我们的mysql开发数据库是单独一台机器部署的,所以认为可能是网络连 ...
- 字符串模板替换方法 MessageFormat.format
String content = "ab,cc,{名称},{密码},{日期},dd,ff"; String array[] = {userName, password, forma ...
- 决战大数据之三-Apache ZooKeeper Standalone及复制模式安装及测试
决战大数据之三-Apache ZooKeeper Standalone及复制模式安装及测试 [TOC] Apache ZooKeeper 单机模式安装 创建hadoop用户&赋予sudo权限, ...