我试图获取对象的名称,并在定义后将其放入数组中,我尝试执行此代码,但名称最终undefined
毫无帮助吗?
function command(category, help, callback) {
this.category = category;
this.help = help;
this.do = callback;
cmndlist[category].push(this.name);
};
答案 0 :(得分:1)
对象没有名称或name
属性(除非您自己添加一个)。如果您引用的是引用对象的变量名,则无法访问。
答案 1 :(得分:0)
假设您创建了一个名为“ foo”的对象:
var foo = new command("category", "help", "callback");
如果要将foo
添加到数组cmndlist[category]
,则只需使用this
:
cmndlist[category].push(this.objectName);
它将起作用!