看了这篇我也懂了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 @   忘川信使  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示