JavaScript Patterns 5.8 Chaining Pattern
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
- save some typing and create more concise code that almost reads like a sentence.
- 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的更多相关文章
- 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.1 Namespace Pattern
global namespace object // global object var MYAPP = {}; // constructors MYAPP.Parent = function() { ...
- 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) ...
随机推荐
- QTableWidget控件总结
[1]QTableWidget简介 QTableWidget是QT对话框设计中常用的显示数据表格的控件. 学习QTableWidget就要首先看看QTableView控件(控件也是有”家世“的!就像研 ...
- 15天玩转redis —— 第七篇 同事的一次缓存操作引起对慢查询的认识
上个星期同事做一个业务模块,需要将一个80M的数据存入到redis缓存中,想法总是好的,真操作的时候遇到了HSet超时,我们使用的是C#的 StackExchange.Redis驱动. <red ...
- [范例] Firemonkey TForm 实现 OnMouseLeave 事件 (適用 Win & OS X)
在 Firemonkey 的 TForm 并没有提供 OnMouseLeave 的事件,不过可以透过 OnMouseMove 来达到相同效果,请见代码: uses FMX.Consts; proced ...
- JS中跨域和沙箱的解析
先来直接分析源码,如下: <!DOCTYPE HTML><html><head> <meta charset="UTF-8"/> & ...
- MySQL日期处理(笔记)
对于刚学习MySQL的人日期的处理是个大坑. 这里整理了如何处理常见的日期问题. (这只是我在学习过程中关于日期处理方面知识的总结,会有一些错误和片面的地方.) Date 函数 函数 描述 NOW() ...
- Java中查找文件并且打印输出指定文件下面的子目录
package com.immoc; import java.io.File; import java.io.IOException; public class FileUtile { //列出fil ...
- TestNG官方文档中文版(4)-运行TestNG
4 - 运行TestNG TestNG可以以不同的方式调用: * Command line * ant * Eclipse * IntelliJ's IDEA 1) 命令行 假 ...
- Java经典实例:进阶版堆栈实现,支持任何对象类型
支持任何对象类型,有更多的错误检查. package Stack; /** * Created by Frank */ public class MyStack<T> implements ...
- mysql经纬度查询并且计算2KM范围内附近用户的sql查询性能优化实例教程
之前很傻很天真地以为无非就是逐个计算距离,然后比较出来就行了,然后当碰到访问用户很多,而且数据库中经纬度信息很多的时候,计算量的迅速增长,能让服务器完全傻逼掉,还是老前辈的经验比我们丰富,给了我很大的 ...
- Wrangle – 响应式的,触摸友好的多选插件
Wrangle 是一个响应式,触摸友好的选择插件,支持 jQuery 以及 Zepto.Wrangle 为多项选择提供了一个独特的方法:通过画一条贯穿项目的线条来选择项目.它给你的应用程序的一种新的方 ...