0

js class this在new与静态引用时所指不同

 7 months ago
source link: https://qidizi.github.io/js-class-this/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

js class this在new与静态引用时所指不同

this在这里的表现也跟func样跟着谁说叫谁老板的,与function.call(“this”概念一样。

简单的一个类

class a{
static a = "static-a";
 b = "ins-b";

constructor(){
console.log('new',this.a,this.b,this);
}

static fn(){
console.log('static',this.a,this.b,this);
}

}

  • new用法new a();时输出如后:new undefined ins-b a {b: "ins-b"},选择性失明,static的东西与被抛弃了;
  • static用法:a.fn();,输出结果如下:
static static-a undefined class a{
static a = "static-a";
 b = "ins-b";

constructor(){
console.log('new',this.a,this.b,this);
}

static fn(){
console.log('static',this.a,this.b,this);
}

}

同样,new的东西被选择性抛弃;


ok,通过上面2个例子,我们可以明确了解到,this,就是指向当前这个使用的对象如new出来的实例,或是static

发表于 2019.07.16 14:50:24

前篇:sftp命令连接时失败码为1解决思路

后篇:smtp经过http代理


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK