blob: a227562a173690914e7327110fb1273d0379c399 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/* shows how to use call() and apply()
* essentially give a method called .bar(), instead of calling it directly with
* use of dobule parens we append .call(instance [, arg1, arg2, ...]), where
* apply takes array of args
*/
function foo() {
this.tag = 'bar';
this.bar = function(txt) {
console.log(txt + this.tag);
}
}
var bar = new foo();
bar.bar('foo')
/* first arg of call is the class instance, could be 'this' if we were in one */;
bar.bar.call(bar, 'foo');
/* apply is similar but it accepts on array of arguments */
bar.bar.apply(bar, ['foo']);
|