使用构造函数获取已定义对象的名称 [英] Getting the name of a defined object with a constructor

查看:63
本文介绍了使用构造函数获取已定义对象的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取对象的名称,并在定义后将其放入数组中,我尝试执行此代码,但是名称最终变为 undefined

I'm trying to get the name of an object and put it in an array after it's defined, I tried doing this code, but the name ended up being undefined any help?

function command(category, help, callback) {
  this.category = category;
  this.help = help;
  this.do = callback;

  cmndlist[category].push(this.name);
}; 


推荐答案

对象没有名称或 name 属性(除非您自己添加一个)。如果您引用的是引用对象的变量名,则无法访问。

Objects do not have a name or name property (unless you add one yourself). If you're referring to the variable name that references the object, that is not possible to access.

这篇关于使用构造函数获取已定义对象的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆