看了这篇我也懂了this

// this指向
// 1.指向全局变量window
var a = 1;

function func() {
var a = 2;
console.log(this.a);
}
func(); //2
// 2.指向调用它的对象(funx()中的this只对直属上司(直接调用者obj)负责,不管有多少个。)
var b = 3;

function funx() {
console.log(this.b);
}
var obj = {
b: 4,
funx
}
obj.funx(); //4

var aa = 15

function test() {
console.log(this.aa)
}
var obj = {
aa: 2,
test
}
var obj0 = {
aa: 3,
obj
}
obj0.obj.test()
// 3.可以将其传给一个变量,并且一样调用它
var d = 1

function test() {
console.log(this.d)
}
var obj = {
d: 2,
test
}
var testCopy = obj.test
testCopy();
// 4.call(),命令指向谁
var e = 7;

function test1() {
console.log(this.e)
}
var obj = {
e: 8,
test1
}
var testCopy = obj.test1
testCopy.call(obj);
// 5.指向构造函数实例化对象

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120126852

posted @ 2022-07-26 17:56  忘川信使  阅读(14)  评论(0编辑  收藏  举报