JavaScript Patterns 4.6 Immediate Object Initialization
( {
// here you can define setting values
// a.k.a. configuration constants
maxwidth : 600,
maxheight : 400,
// you can also define utility methods
gimmeMax : function() {
return this.maxwidth + "x" + this.maxheight;
},
// initialize
init : function() {
console.log(this.gimmeMax());
// more init tasks...
}
}).init();
Usage
protect the global namespace while performing the one-off initialization tasks.
Note
If you want to keep a reference to the object after it is done, you can easily achieve this by adding return this; at the end of init().
JavaScript Patterns 4.6 Immediate Object Initialization的更多相关文章
- JavaScript Patterns 5.7 Object Constants
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...
- 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.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- JavaScript Patterns 5.8 Chaining Pattern
Chaining Pattern - Call methods on an object one after the other without assigning the return values ...
随机推荐
- 在SQL Server里如何进行页级别的恢复
在今天的文章里我想谈下每个DBA应该知道的一个重要话题:在SQL Server里如何进行页级别还原操作.假设在SQL Server里你有一个损坏的页,你要从最近的数据库备份只还原有问题的页,而不是还原 ...
- Gradle学习系列之三——读懂Gradle语法
在本系列的上篇文章中,我们讲到了创建Task的多种方法,在本篇文章中,我们将学习如何读懂Gradle. 请通过以下方式下载本系列文章的Github示例代码: git clone https://git ...
- python学习笔记 - 初识socket
socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- docker与虚拟机性能比较
http://blog.csdn.net/cbl709/article/details/43955687www.chenbiaolong.com 概要 Docker是近年来新兴的虚拟化工具,它可以和虚 ...
- OAuth2.0 基础概述
web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...
- java之StringBuilder类详解
StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...
- 划分树---Feed the dogs
POJ 2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...
- 【Asphyre引擎】关于AsphyreTypes中OverlapRect的改动,都是泪啊!!!
OverlapRect改动:两个参数对调了.想问问LP,这样真的好吗? Sphinx304版本的代码: function OverlapRect(const Rect1, Rect2: TRect): ...
- YII 的基本CURL操作
Order::model()->updateAll(array('merchant_id'=>'bbb'), "id in ('140868169311','1408690584 ...
- 移除NDK方法
以下内容由:于伟建 提供 删除.project中的以下字段基本都是有cdt关键字的位置 删除.cproject然后重启eclipse,clean,重新编译我这里还有错误,就手动删了gen 删除包含cd ...