function globalTest() { this.name = "global this"; console.log(this.name); } globalTest(); //global this |
var name = "global this"; function globalTest() { console.log(this.name); } globalTest(); //global this |
var name = "global this"; function globalTest() { this.name = "rename global this" console.log(this.name); } globalTest(); //rename global this |
function showName() { console.log(this.name); } var obj = {}; obj.name = "ooo"; obj.show = showName; obj.show(); //ooo |
function showName() { this.name = "showName function"; } var obj = new showName(); console.log(obj.name); //showName function |
var name = "global name"; function showName() { this.name = "showName function"; } var obj = new showName(); console.log(obj.name); //showName function console.log(name); //global name |
var value = "Global value"; function FunA() { this.value = "AAA"; } function FunB() { console.log(this.value); } FunB(); //Global value 因为是在全局中调用的FunB(),this.value指向全局的value FunB.call(window); //Global value,this指向window对象,因此this.value指向全局的value FunB.call(new FunA()); //AAA, this指向参数new FunA(),即FunA对象 FunB.apply(window); //Global value FunB.apply(new FunA()); //AAA |