this面试题

var a = "outer";
let obj = {
	a: "inner",
	foo: () => {
		console.log(this.a);
	},
};
obj.foo();

class Foo {
	print = () => console.log(this.x);
	constructor() {
		this.x = 1;
	}
}
let foo = new Foo();
foo.print.call({ x: 2 });

// 箭头函数与this - 代码段3
function printThis() {
	let print = () => console.log(this);
	print();
}
printThis.call([1]);
printThis.call([2]);

var _name = "global";
var obj = {
	func() {
		const innerFunc = () => console.log(this._name);
		return innerFunc;
	},
	_name: "local",
};

// 语句1
obj.func()(); // var fn = obj.func()  this->obj  local
// 语句2
var func = obj.func;
func()(); // this-> window  global
// 语句3
obj.func.bind({ _name: "newObj" })()(); // let fn = obj.func.bind({ _name: "newObj" })()  newObj
// 语句4
obj.func.bind()()(); // window  global

你可能感兴趣的:(前端,javascript,vue.js)