Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpeng/f732974a39282bf033a2 to your computer and use it in GitHub Desktop.
Save karenpeng/f732974a39282bf033a2 to your computer and use it in GitHub Desktop.
function Car(name) {
this.name = name;
}
Car.prototype.display = function () {
console.log(this.name);
}
var benz = new Car('benz');
benz.display(); // benz, this === benz
var bmw = new Car('bmw');
// benz.display.call(bmw); // bmw, 这个时候,benz.display 这个方法的 this 被改变了,this === bmw
// benz.display.apply(bmw); // 同上
// var diplayBMW = benz.display.bind(bmw);
// displayBMW(); // bmw, this === bmw now
benz.display.call({name: 'olo'})
benz.display()
benz.display.apply({name: 'olo1'})
benz.display()
benz.display.bind({name: 'olo2'})
benz.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment