Chaining Pattern - Call methods on an object one after the other without assigning the return values of the previous operations to variables and without having to split your calls on multiple lines.

var obj = {

    value: 1,

    increment: function () {

        this.value += 1;

        return this;

    },

    add: function (v) {

        this.value += v;

        return this;

    },

    shout: function () {

        alert(this.value);

    }

};

// chain method calls

obj.increment().add(3).shout(); //

// as opposed to calling them one by one

obj.increment();

obj.add(3);

obj.shout(); //

Pros and Cons of the Chaining Pattern

Pros

  1. save some typing and create more concise code that almost reads like a sentence.
  2. splitting your functions and creating smaller, more specialized functions, as opposed to functions that try to do too much. This improves the maintainability in the long run.

Cons

  debug code written this way which it's hard to check which part is wrong in the same line.

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 5.8 Chaining Pattern的更多相关文章

  1. JavaScript Patterns 5.5 Sandbox Pattern

    Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...

  2. JavaScript Patterns 5.4 Module Pattern

    MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...

  3. JavaScript Patterns 5.1 Namespace Pattern

    global namespace object // global object var MYAPP = {}; // constructors MYAPP.Parent = function() { ...

  4. JavaScript Patterns 4.2 Callback Pattern

    function writeCode(callback) { // do something... callback(); // ... } function introduceBugs() { // ...

  5. JavaScript Patterns 2.6 switch Pattern

    Principle • Aligning each case with switch(an exception to the curly braces indentation rule). • Ind ...

  6. 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, ...

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

  8. JavaScript Patterns 6.7 Borrowing Methods

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

  9. JavaScript Patterns 6.5 Inheritance by Copying Properties

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

随机推荐

  1. avalon.js 1.4.6简单列表数据绑定ms-repeat ms-click

    1.列表数据绑定 <html> <head> <meta charset="UTF-8"> <meta name="viewpo ...

  2. C#网站发布在IIS10上,Access数据库读取为空白的解决方案

    打开IIS10,进入应用程序池,右边“设置应用程序池默认设置”中,启用32位应用程序,False改为True即可.

  3. 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板

    [源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ ...

  4. Oracle数据库,模糊查询、去重查询

    分组去重查询,并执行某一个函数 :select  分组字段,聚合函数 from 表名 where 条件 group by分组字段 select 分组字段,聚合函数 from 表名 where 条件 g ...

  5. html中代码高亮显示

    <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> ...

  6. 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...

  7. 为什么正常安装并成功运行了Genymotion虚拟但是运行的时候启动的却是自带的模拟器?

    最近因为这个问题困惑了好久,最终找到了解决思路: 点击genymotion——setting——ADB——Use sustom Android tools,找到电脑中SDK文件位置就可了! 希望自己坚 ...

  8. No.016:3Sum Closest

    问题: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. CentOS安装zip unzip命令

    yum install zip unzip

  10. Android应用开发基础之十:多媒体编程

    多媒体概念 文字.图片.音频.视频 计算机图片大小的计算 图片大小 = 图片的总像素 * 每个像素占用的大小 单色图:每个像素占用1/8个字节 16色图:每个像素占用1/2个字节 256色图:每个像素 ...