JavaScript系列—变换莫测的this

前言

相关知识讲解:

- 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 非对象

情况一

 情况二

情况三 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值