一网打尽this,对执行上下文说Yes

	**话不多说,直接上**

1:全局中的this

function fn(){
	console.log(this)
}
function fn2(){
	"use strict"
	console.log(this)
}
fn()  // window
fn2()   // undefined

结论:正常情况下,全局this都是指向window,而在严格模式下this指向undefined

2对象中的this

const foo = {
	bar: 10,
	fn: function () {
		console.log(this)
		console.log(this.bar)
	}
}
let fn1 = foo.fn()
fn1()
两个log会分别打印 window 和 undefined
结论:foo.fn()情况下this指向foo。而把它赋值给一个全局变量fn1时,那this就会指向window

************************************分割*********************************

稍作改动一下:
const foo = {
	bar: 10,
	fn: function () {
		console.log(this)
		console.log(this.bar)
	}
}
foo.fn()
两个log会分别打印 {bar:10, fn()}10

3上下文中this

const student = {
	name: '张三',
	fn: function () {
		return this
	}
}
console.log(student.fn() === student)    // true
return之后,指向一样了

************************************分割*********************************

const student = {
	name: '张三',
	person:{
		name:'李四',
		fn: function (){
			return this.name
		}
	}
}
console.log(student.person.name)    // 李四

在嵌套关系中,this也指向最后一个调用者。return之后this指向person

************************************分割*********************************

好,下面请猜一下 下面代码运行结果

const one = {
	text: 'one',
	fn: function(){
		return this.text
	}
}

const two = {
	text: 'two',
	fn: function(){
		return one.fn()
	}
}

const three = {
	text: 'three',
	fn: function(){
		let fn = one.fn
		return fn()
	}
}

console.log(one.fn())   // one
console.log(two.fn())   // one
console.log(three.fn())  // undefined

第一个最简单,不再解释
第二个 tow.fn()的最终调用还是 one.fn() ,因此结果也依然是 one
第三个 采用了let fn = one.fn的赋值进行了"裸奔"调用,
因此这里的this指向了window,全局中没有.text,所以是undefined

************************************分割*********************************

const obj = {
	text: 'obj',
	fn: function(){
		return this.text
	}
}

const obj2 = {
	text: 'obj2',
	fn: obj.fn
}
console.log(obj2.fn())   // obj2

最后打印时将fn()挂载到obj2上,所以this也指向了obj2

4通过bind,apply,call改变this

用代码总结,下面三段代码可以说是等价的

call / apply / bind 他们会将调用他们对象的this指向参数

const target = {
	fn.call(taiget, 'alg1','alg2')
}

const target = {
	fn.apply(target, ['alg1', 'alg2']
}

const target = {
	fn.bind(target, 'alg1', 'alg2')
}

************************************分割*********************************

const foo = {
	name:'张三',
	fn: function (){
		console.log(this.name)
	}
}

const bar = {
	name: '李四'
}
console.log(foo.fn.call(bar))      // 李四

5构造函数中的this

function Foo(){
	this.bar = '张三'
}
let obj = new Foo()
console.log(obj.bar)     // 张三

关于new操作符做了什么
1:创建一个对象
2:将构造函数的this指向该对象
3:为这个对象添加属性和方法
4:返回这个对象
也可以用代码表示

let obj = {}
obj.__proto__ = Foo.prototype
Foo.call(obj)

************************************分割*********************************

构造函数中出现return1:
function Foo(){
	this.user = '张三'
	const obj = {}
	return obj
}
const inst = new Foo()
console.log(inst.user)     // undefined
因为 return的是obj空对象

2:
function Foo(){
	this.user = '张三',
	return 1
}
const inst = new Foo()
console.log(inst.user)    // 张三return一个值,或者是一个对象(复杂类型),那this就指向返回的这个对象
如果是基本类型,this仍然指向实例

6箭头函数中的this

*箭头函数中,this的指向由外层(函数/全局)作用域决定*

const foo = {
	fn: function(){
		/*window.*/setTimeout(function(){
			console.log(this)  // window
		})
	}
}
foo.fn() 


const foo = {
	fn: function(){
		setTimeout(()=>{
			console.log(this)     // foo
		})
	}
}
foo.fn()

单纯的this指向就很简单,如果综合所有情况并结合this优先级,就很难搞了

7this优先级

当把call,apply,bind,newthis绑定成为显示绑定
而根据调用关系确定this指向的情况称为隐士绑定

上代码

function foo(a){
	console.log(this.a)
}

const obj1 = {
	a: 1,
	foo: foo
}

const obj2 = {
	a: 2,
	foo: foo
}

obj1.foo.call(obj2)     // 2
obj2.foo.call(obj1)     // 1

可见 call apply 的显示绑定,一般情况下优先级较高


************************************分割*********************************

function foo(a){
	this.a = a
}
const obj1 = {}
let bar = foo.bind(obj1)
bar(2)
console.log(obj1.a)      //2

foo使用bind将this指向了obj1,而后又赋值给了bar,并修改值为2. 织自然打印值为2

结合上面代码

let bar2 = new bar(3)
console.log(bar2.a)     // 3

bar是通过bind方法构造的函数,this指向了obj1。
当它再次当做构造函数通过new调用时,返回的实例会和obj1解绑。
也就是new的绑定修改了bind绑定中this的指向。new比bind优先级高


************************************分割*********************************


function foo(){
	return a => {
		console.log(this.a)
	}
}
const obj1 = {
	a: 2
}
const obj2 = {
	a: 3
}
const bar = foo.call(obj1)    
console.log(bar.call(obj2))    // 2

为什么是2呢?

foo中的this绑定到了obj1上。所以bar引用的就是箭头函数中的this了,而这个this也绑定到了obj1上
但是箭头函数中的绑定是不能被修改的  (还不理解的可以百度一下详细)

如果写成这样

let a = 123
const foo = () => a => {
	console.log(this.a)
}
const obj1 = {
	a: 2
}
const obj2 = {
	a: 3
}
const bar = foo.call(obj1)    
console.log(bar.call(obj2))    // 123

但如果把 let a = 123  改成   const a = 123   想一下会怎么样

当const声明的常量时,不会挂载到window全局对象上。自然this指向window,也找不到挂载不了window上的const

8例题,实现一个bind函数

this的指向涉及的规范繁多,优先级也很混乱

面试时,被故意刁难很难脱颖而出

这里一个典型的题目: 实现一个bind函数

Function.prototype.bind = Function.prototype.bind || function(context){
	let that = this
	let args = Array.prototype.slice.call(arguments, 1)
	return function bound(){
		let innerArgs = Array.prototype.slice.call(arguments)
		let finalArgs = args.concat(innerArgs)
		return that.apply(context, finalArgs)
	}
}

这样的实现较为很好,相比之前在this优先级分析那里展示的规则: bind返回的函数如果作
为构造函数new关键字的话。绑定的this就会被 ‘忽略’

为实现这样规则,只需考虑两种调用方式。 在bound函数中进行this.instanceof判断

另一个细节是 函数又length属性,表示形参个数。上面代码中形参的个数显然会失真。
改进实现方式就是对length属性进行还原,难点也在于length属性值是不可重写的

这一般属于‘超纲’内容!
~
漏了一块
就在这里补齐了

function setName(obj){
	obj.name = '张三'
	obj = new Object()
	obj.name = '李四'
}
let person = new Object()
setName(person)
console.log(person.name)    // 张三

看似没有this

至于打印的是张三,就是因为obj在函数内部重写之后,变成了指向本地的指针,会随着函数执行完毕而销毁
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值