forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaining.html
More file actions
37 lines (34 loc) · 691 Bytes
/
chaining.html
File metadata and controls
37 lines (34 loc) · 691 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!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>