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. 介绍开源的.net通信框架NetworkComms框架 源码分析(二)ConnectionInfo

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  2. [iOS] Delphi for iOS 版本需求

    Delphi iOS 版本需求 版本 版本需求 官网 备注 Delphi 10.1 Berlin iPod Touch, iPhone, or iPad iOS 8 iOS 9 http://docw ...

  3. 导出 XE6 预设 Android Style (*.style) 档案

    如果想要修改 Android Style 可以将它导出成 *.style 后再加入 TStyleBook 内来修改(iOS 的方法亦同):

  4. EasyUI-扩大在DataGrid显示次网格的行

    一.下载并引用:datagrid-detailview.js脚本文件 二.添加UrlInfo控制器,添加Index页面代码如下: @{ Layout = null; } <!DOCTYPE ht ...

  5. 使用Apache的DigestUtils类实现哈希摘要(SHA/MD5)

    包名称:org.apache.commons.codec.digest 类名称:org.apache.commons.codec.digest.DigestUtils 1.MD5 public sta ...

  6. Atitit.事件机制 与 消息机制的联系与区别

    Atitit.事件机制 与 消息机制的联系与区别 1. 消息/事件机制是几乎所有开发语言都有的机制,在某些语言称之为消息(Event),有些地方称之为(Message).1 2. 发布/订阅模式1 3 ...

  7. Asp.net 实现Session分布式储存(Redis,Mongodb,Mysql等) sessionState Custom

    对于asp.net 程序员来说,Session的存储方式有InProc.StateServer.SQLServer和Custom,但是Custom确很少有人提及.但Custom确实最好用,目前最实用和 ...

  8. WPF如何实现一个漂亮的页签导航UI

    最近看到一个比较漂亮的UI主界面,该UI是用左边的页签进行导航,比较有特色,就想着尝试用WPF来实现一下.经过一番尝试,基本上将UI设计图的效果用WPF程序进行了实现.下面介绍一下主要的思路: 1 U ...

  9. 20个最新的照片 PS 技巧,提升摄影水平

    相信很多人都知道 Photoshop 在照片编辑方面的强大,所以几乎每张照片经过 PS 处理后都可以变成一个真正的杰作.这里分享的这组 Photoshop 教程可以帮助你学习到新的照片处理技术.你会发 ...

  10. CSS常用样式(四)之animation

    上篇CSS常用样式(三)这篇博文中已经介绍过了CSS中具有动画效果的transition.transform,今天来大概说说CSS中的animation.animation的加入会使得动画效果更加乐观 ...