以下は私のコードです -
(function($){
obj = {
alertText: function(){
console.log('Called');
},
testFunc: function(){
console.log(this);
this.alertText();
},
checkFunc: function(){
inner();
function inner(){
console.log(this);
this.alertText();
}
}
}})(jQuery)
を呼び出すとtestFunc()
、thisキーワードalertText()
を介して適切に呼び出されます。
ただし、関数を呼び出した後、これalertText()
を使用する呼び出しは内部で失敗します(this.alertText は関数ではないことを示す TypeError) 。inner()
checkFunc()
上記のようにこれをコンソールすると、その中にさまざまなコンテンツが表示されます.1つtestFunc()
は object を表示obj
し、1つinner()
は object を表示しWindow
ます。
これはなぜですか?これが 2 つの場所で異なる意味を持つのはなぜですか?