admin管理员组文章数量:1417720
I am executing following code
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();
getting following output :
qq John
ww Person
I am not getting how am getting this.name = Person in second console
I am executing following code
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();
getting following output :
qq John
ww Person
I am not getting how am getting this.name = Person in second console
Share Improve this question asked Dec 22, 2017 at 6:10 Javed INJaved IN 3321 gold badge4 silver badges12 bronze badges 03 Answers
Reset to default 3That es from Function.name
as explained in the JS MDN
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"
If you want to get the desired output, change the property name
to name1
function Person(name1, age){
this.name1 = name1 || "John";
this.age = age || 24;
this.displayName = function(){
console.log('qq ',this.name1);
}
}
Person.name1 = "John";
Person.displayName = function(){
console.log('ww ',this.name1);
}
function main() {
var person1 = new Person('John');
person1.displayName();
Person.displayName();
}
The output :
qq John
ww John
The name property returns the name of a function statement.
When you are calling function as Person.displayName(); & try to use "this.name". it will return name of the function
本文标签: closuresJavascriptgetting function name with thisnameStack Overflow
版权声明:本文标题:closures - Javascript : getting function name with this.name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745267263a2650677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论