[英]How to get the name of the new object's constructor in the function? (Javascript)
I'm really new to the JavaScript sooo... 我是JavaScript的新手......
I have a class: 我有一节课:
var warrior = function(HP) {
this.health = HP;
}
And when I create a new warrior: 当我创造一个新的战士时:
var somebody = new warrior(50);
I'd like to give the name "somebody" to the new element. 我想给新元素命名为“某人”。 How can I do it?
我该怎么做?
You cannot. 你不能。 Variable names are meaningless to the program functionality.
变量名对程序功能毫无意义。 If you care about the name, use a string and pass it as an argument.
如果您关心名称,请使用字符串并将其作为参数传递。
Something like this maybe: 这样的事情可能是:
var warrior = function(HP) { this.health = HP; } warrior.prototype.name; var somebody = new warrior(50); somebody.name = 'somebody'; console.log(somebody);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.