function base() {
this.member = "global";
this.method = function() {
console.log("global method");
return "global method return";
};
}
(function extend() {
base.call(this);
console.log(this.member);
console.log(this.method());
})();
其打印结果为什么会是:global
global method
global method return呢,还望高手不吝赐教,正确的打印结果不应该是global method
global
global method return
吗?
function base() {
this.member = "global";
this.method = function() {
console.log("global method");
return "global method return";
};
} (function extend() {
base.call(this);
console.log(this.member); //这句输出global ,因为base函数的this.member 就是 "global";
console.log(this.method()); //这句输出 global method global method return //lz你可以把这句注释掉就清楚了,
})();
</script>
global
global method return已经帮你加了注释,再搞不明白就。。
function base() {
this.member = "global";
this.method = function() {
console.log("global method");
return "global method return";
};
}
(function extend() {
base.call(this); // 这里的this===window,调用base设置window.member和window.method
console.log(this.member); // 这里的this.member即window.memeber值为global有问题吗?
console.log(this.method()); //调用window.method,得执行函数体内的console.log("global method");再然后打印函数的返回值"global method return";
})();
...
})();首先你要明白这是一个自执行函数形式 所以你不用调用即可输出
然后明白call的作用 顺带着 apply你也要了解下 并且明白他们的区别至于怎么了解call 和apply please google一下