gpt4 book ai didi

Javascript : getting function name with this. 名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:41:04 25 4
gpt4 key购买 nike

我正在执行下面的代码

function Person(name, age){
this.name = name || "John";
this.age = age || 24;
this.displayName = function(){
console.log('qq ',this.name);
}
}

Person.name = "John";
Person.displayName = function(){
console.log('ww ',this.name);
}

var person1 = new Person('John');
person1.displayName();
Person.displayName();

得到以下输出:

qq  John
ww Person

我不明白如何在第二个控制台中获取 this.name = Person

最佳答案

JS MDN 中所述,它来自 Function.name

A Function object's read-only name property indicates the function's name as specified when it was created, or "anonymous" for functions created anonymously.

function doSomething() {}
doSomething.name; // "doSomething"

关于Javascript : getting function name with this. 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936644/

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