this 的理解

this 定義

this: 指的是調(diào)用 當前 方法(函數(shù))的那個對象
誰調(diào)用該方法this就指令

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>

<script>

// this : 這個
// this: 指的是調(diào)用 當前 方法(函數(shù))的那個對象

function fn1(){
    // this
}
// fn1();           this => window
// oDiv.onclick = fn1;          this => oDiv
/*
oDiv.onclick = function (){
    fn1();                  fn1() 里的this => window
};

<div onclick="    this     fn1();      "></div>     fn1(); 里的 this 指的是 window
*/
// alert( this );       // object window

// window 是 JS “老大”
// window.alert( this );

function fn1(){
    alert( this );              // window
}
// fn1();
// window.fn1();
</script>

</head>

<body>

<input id="btn1" type="button" value="按鈕" />

<input id="btn2" type="button" onclick=" fn1(); " value="按鈕2" />

<script>
var oBtn = document.getElementById('btn1');
// oBtn.onclick = fn1;
oBtn.onclick = function (){
    // this
    fn1();
};
</script>

</body>
</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容