( {

    // 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的更多相关文章

  1. JavaScript Patterns 5.7 Object Constants

    Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...

  2. 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 ...

  3. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  4. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  5. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  6. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  7. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  8. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  9. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

随机推荐

  1. 2014.7.12 敏捷个人奥森健步走&敏友分享会.活动报道

    今天是个阳光明媚的日子,正式开起了敏捷个人2014年每月一次的健步走&读书分享活动. 周老师一大早8点就到了森林公园南门,一边看书一边等候敏友们的到来.时间走到了9点半,大家基本到齐了,我们准 ...

  2. Http客户端识别的方法

      Http用户识别的机制方法分为如下几种: 1).承载用户身份的HTTP首部 2).客户端IP地址跟踪,根据客户端IP地址进行识别 3).用户登录,用认证方式设别用户 4).使用胖URL,一种在UR ...

  3. C#使用基类的引用 and 虚方法和覆写方法

    结论:使用基类的引用,访问派生类对象时,得到的是基类的成员. 虚方法和覆写方法

  4. 重构第30天 尽快返回 (Return ASAP)

    理解:把条件语句中复杂的判断用尽快返回来简化. 详解:如首先声明的是前面讲的”分解复杂判断“,简单的来说,当你的代码中有很深的嵌套条件时,花括号就会在代码中形成一个长长的箭头.我们经常在不同的代码中看 ...

  5. JSChart_页面图形报表

    首先在页头的"head"中加上: $(document).ready(function() { //myData与colors变量  是做演示用的,可以直接赋值给myChart就可 ...

  6. 实现GridView翻页并且实现CheckBox选中功能的保持

    在GridView与数据库进行绑定后,由得到的数据记录可能有许多条,以至一个页面无法容纳,这时需要进行多页显. 要实现分页显现,只要使用分页类 "PagedDataSource" ...

  7. [moka学习笔记]yii2.0 rules的用法(收集,不定期更新)

    public function rules(){ return [ ['title','required','message'=>'标题不能为空'], ['title','string','mi ...

  8. [moka同学笔记]YII2中发送邮件示例(转载)

    原文:http://yiilib.com/topic/675/Yii2%E4%B8%AD%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E7%A4%BA%E4%BE%8B { ...

  9. 利用PBFunc在Powerbuilder中解析Json对象

    利用PBFunc工具在Powerbuilder解析json,只需要调用getattribute方法来获取 解析unicode格式的json: n_pbfunc_json lnv_json lnv_js ...

  10. 自定义View_2_关于自定义组合View

    自定义View(2) Android当中给我们提供了丰富的UI控件,当然也许满足不了我们的需求,我们就必须学会自定义自己的View,我们怎么算是自定义自己的view呢! 我们会根据原来有的View对V ...