再來玩玩this是指向哪裡吧
this 意指當前的function是屬於哪個物件。
e.g.
var ary = [1, 2, 3]; // var ary = [1, 2, 3]; === window.ary = [1, 2, 3];
ary.a = 12;
ary.show = function() {
console.log(this.a); // this === ary
};
ary.show();
e.g.
document.onclick = function() {
console.log(this); // this === document
};
e.g.
function show() {
console.log(this); // this === window
}
show(); // show() === window.show();
e.g.
window.show = function() {
console.log(this); // this === window
};
show();
e.g.
var ary = [1, 2, 3]; // var ary = [1, 2, 3]; === window.ary = [1, 2, 3];
ary.a = 12;
ary.show = function() {
console.log(this.a); // this === ary
};
ary.show();
e.g.
document.onclick = function() {
console.log(this); // this === document
};
e.g.
function show() {
console.log(this); // this === window
}
show(); // show() === window.show();
e.g.
window.show = function() {
console.log(this); // this === window
};
show();
留言
張貼留言