我正在执行下面的代码
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/