Arrow Funtion

88 阅读1分钟

箭头函数this指向父级函数的作用域,而不是它所在的函数

let a = {
	name: '请大家多多三连',
    init: function() {
    	console.log(this)
    }
}

a.init()

let b = {
	name: '再次请大家多多三连',
    init: () => {
    	// this指向的是它所在的对象的【父级】—— Window
    	console.log(this)
    }
}

b.init()
(1)
let fn = () => {
    // 指向Window
    console.log(this)
    // 指向Window的constructor
    console.log(this.constructor)
    // 打印undefined
    console.log(this.prototype)
}

(2)
// 箭头函数不能使用new
let fn = () => '请大家接着三连';
new fn();

// Uncaught TypeError: fn is not a constructor;