-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
borrowing-methods.html
83 lines (67 loc) · 2 KB
/
borrowing-methods.html
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Borrowing Methods
Description: reuse one or two methods of an existing object without forming a parent-child relationship with that object
*/
function f() {
var args = [].slice.call(arguments, 1, 3);
return args;
}
var one = {
name:'object',
say:function (greet) {
return greet + ', ' + this.name;
}
};
// test
console.log(one.say('hi')); // "hi, object"
var two = {
name:'another object'
};
// apply two to original object one.
console.log(one.say.apply(two, ['hello'])); // "hello, another object"
// assigning to a variable
// `this` will point to the global object
var say = one.say;
console.log(say('hoho')); // "hoho, undefined"
// passing as a callback
var yetanother = {
name:'Yet another object',
method:function (callback) {
return callback('Hola');
}
};
console.log(yetanother.method(one.say)); // "Hola, undefined"
function bind(o, m) {
return function () {
return m.apply(o, [].slice.call(arguments));
};
}
var twosay = bind(two, one.say);
console.log(twosay('yo')); // "yo, another object"
// ECMAScript 5 adds a method bind() to Function.prototype, making it just as easy to use as apply() and call().
if (typeof Function.prototype.bind === 'undefined') {
Function.prototype.bind = function (thisArg) {
var fn = this,
slice = Array.prototype.slice,
args = slice.call(arguments, 1);
return function () {
return fn.apply(thisArg, args.concat(slice.call(arguments)));
};
};
}
var twosay2 = one.say.bind(two);
console.log(twosay2('Bonjour')); // "Bonjour, another object"
var twosay3 = one.say.bind(two, 'Enchanté');
console.log(twosay3()); // "Enchanté, another object"
// reference
// http://shop.oreilly.com/product/9780596806767.do
</script>
</body>
</html>