Permalink
Find file
f1b4f9a Feb 1, 2012
37 lines (34 sloc) 691 Bytes
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Chaining Pattern
Description: it enables you to call methods on an object one after the other
*/
var obj = {
value:1,
increment:function () {
this.value += 1;
return this;
},
add:function (v) {
this.value += v;
return this;
},
shout:function () {
console.log(this.value);
}
};
// chain method calls
obj.increment().add(3).shout(); // 5
// as opposed to calling them one by one
// obj.increment();
// obj.add(3);
// obj.shout();
</script>
</body>
</html>