Last active
August 29, 2015 14:21
-
-
Save karenpeng/f732974a39282bf033a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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