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
| call: var person1 = {firstName: 'Jon', lastName: 'Kuperman'}; var person2 = {firstName: 'Kelly', lastName: 'King'};
function say(greeting) { console.log(greeting + ' ' + this.firstName + ' ' + this.lastName); }
say.call(person1, 'Hello'); say.call(person2, 'Hello');
apply: var person1 = {firstName: 'Jon', lastName: 'Kuperman'}; var person2 = {firstName: 'Kelly', lastName: 'King'};
function say(greeting) { console.log(greeting + ' ' + this.firstName + ' ' + this.lastName); }
say.apply(person1, ['Hello']); say.apply(person2, ['Hello']);
bind: var person1 = {firstName: 'Jon', lastName: 'Kuperman'}; var person2 = {firstName: 'Kelly', lastName: 'King'};
function say() { console.log('Hello ' + this.firstName + ' ' + this.lastName); }
var sayHelloJon = say.bind(person1); var sayHelloKelly = say.bind(person2);
sayHelloJon(); sayHelloKelly();
|