this 的理解

this 定义

this: 指的是调用 当前 方法(函数)的那个对象
谁调用该方法this就指令

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

// this : 这个
// this: 指的是调用 当前 方法(函数)的那个对象

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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容