前言
相关知识讲解:
- javascript中的this
- 全局环境
- 事件处理函数
- 函数内部
- 箭头函数内部
- 对象方法内部
- 构造函数中
- 原型链上函数
- getter 和 setter 中
- 全局环境
- window
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
console.log( this ); //Window
</script>
</body>
</html>
- --
- 事件处理函数
- DOM 事件处理函数
情况一:
情况二
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="box">div</div>
<script>
var box = document.getElementById("box");
box.onclick = function(){
console.log(this)//box
setTimeout( function(){
console.log( this ); //window
this.style.backgroundColor = "red"; //报错
},1000 )
}
</script>
</body>
</html>
- 内联事件处理函数
情况一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- box -->
<div id="box" onclick="console.log( this )" >div</div>
<!-- 特殊情况 此处this指的window -->
<div id="box" onclick="( (function(){console.log( this )})() )" >div</div>
<script>
box.onclick = function(){
console.log( this ); // 事件处理函数中的 this, 该事件由谁触发,this指的就是谁 box
}
</script>
</body>
</html>
- --
- 函数内部
- 函数直接执行
- 非严格模式下
- 默认指向全局对象
- 浏览器
情况一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function fn() {
console.log(this);//window
}
fn();//window
</script>
</body>
</html>
- node
- node交互界面中
- js文件中
- 严格模式下(前面省略window,浏览器严格模式支持)
- undefined
情况一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function fn(){
"use strict"
console.log(this);
}
fn();//undefined
window.fn(); //window
</script>
</body>
</html>
- call,apply和bind
- 把 this 的值从一个执行环境传入另一个执行环境
- call 和 apply 的参数差异
- 如果传入的不是对象会调用相对的构造函数,进行隐式转换
情况一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function fn(m,n){
console.log( this.a,m,n );
}
fn.call( {a: 1},2,3 ); // 1, 2, 3
// 函数.call 会直接调用该函数.
// 并且call 的 第 0个参数就是 函数执行时候的this
// 后面的参数是 函数执行的时候的参数
</script>
</body>
</html>
- bind
- 返回一个新函数,内部this指向被修改
- 只会改变一次
情况一
情况二
情况三
情况四
情况五
- --
- 箭头函数内部
- 与定义时的环境中 this 一致
- 点击元素定时改变
- bind,call 不能修改其内部指向
- 全局中定义箭头函数
- 全局对象
- 对象内函数定义箭头函数
- 对象内函数返回箭头函数
情况一
情况二
情况三
情况三
情况四
情况五
情况六
- --
- 对象方法内部
- 当函数作为对象里的方法被调用时
- 调用该函数的对象
- 对象后续附属上的方法调用
- 调用该函数的对象
- 作为函数内嵌套多层的方法调用
- 就近绑定
情况一
情况二
情况三
情况四
情况五
情况六
- --
- 构造函数中
- 构造函数中没有显式return
- 构造函数中显式return
- return 对象
- return 非对象
情况一
情况二
情况三