gpt4 book ai didi

javascript - 如何在不使用类名的情况下从类中访问类本身

转载 作者:行者123 更新时间:2023-12-03 09:45:19 26 4
gpt4 key购买 nike

<分区>

是否可以在类函数中访问 class 本身:

class MyClass {
static get myFunction() { return "foo"; }

constructor() {
console.log(MyClass.myFunction); // "foo" -- it works when using its name. This is what we want but without using the name
console.log(this.myFunction); //undefined
console.log(this.prototype.myFunction); //Cannot read property 'myFunction' of undefined
}

}

new MyClass();

是否有可能实现与使用 MyClass.myFunction() 相同的效果并在不使用类名的情况下访问 static 方法(在这种情况下没有在这个例子中使用 MyClass

类似this.master.myFunction()的东西(我只是在这里制作master,显然不叫master)

JSBin:https://jsbin.com/hawituxosu/1/edit?js,console

这可能吗?谢谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com