Javascript : getting function name with this. 名称

标签 javascript closures

我正在执行下面的代码

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/

相关文章:

使用 Object.create 创建的 Javascript 数组 - 不是真正的数组?

javascript - 从文本字符串中获取变量 [jQuery]

javascript - 如何覆盖 Prototype.js 类中声明的 javascript 函数

JavaScript 闭包和内存管理

Javascript 可链接闭包

swift - 将可变参数闭包参数标记为转义

javascript - 图像悬停生成器

javascript - 如何将流程图中的线条组合在一起?

javascript - 在闭包中使用原型(prototype)模式

scala - Akka Actor 、 future 和闭包