let's look at an example ,
var john = {
name: 'john',
job: 'engineer',
age: 35,
presentation: function (style, timeOffDay) {
if (style === 'formal') {
console.log('Good ' + timeOffDay + ',ladies and gentlemen i\'m ' + this.name + ' working as ' + this.job + ' and i\'m ' + this.age + ' years old');
} else if (style === 'friendly') {
console.log('Good ' + timeOffDay + ',ladies and gentlemen i\'m ' + this.name + ' working as ' + this.job + ' and i\'m ' + this.age + ' years old');
}
}
};
john.presentation('formal','morning');
var smith={
name:'smith',
job:'teacher',
age:40
};
john.presentation.call(smith,'friendly','evening');
apply() → arguments as array
john.presentation.apply(smith, ['friendly', 'evening']);
bind()→ creates a new functionvar smithPresentation = john.presentation.bind(smith);
smithPresentation('formal', 'morning');
bind()does not execute immediately.
No comments:
Post a Comment