summaryrefslogtreecommitdiffstats
path: root/samples/call_method.js
blob: 0197723f6233ed92feb83e02a3797bfacfc5c98a (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 given a method called .bar(), instead of calling it directly with
 * use of double 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']);