如何将对象的属性作为变量访问?

发布于 2024-11-03 02:55:16 字数 959 浏览 3 评论 0原文

我有两个对象:

object1={
   type: 'obj1',
   nName: 'nName'
}

object2={
   type: 'obj2',
   pName: 'pName'
}

在我的 js 代码中,我有:

object=GET_OBJECT();

GET_OBJECT() 方法返回 object1object2,然后,我想访问对象的 name 属性,该属性是 nNamepName

我有一种方法可以获取返回对象的名称(pName 或 nName):

function getName(Object, name){
      return object.name;
}

我希望 name 是一个变量,以便我可以访问 pNamenName 以这种方式:

object=GET_OBJECT();

var name='';

if(object.type=='obj1')
   name='nName';
else
   name='pName';

var finalName=getName(object, name);

但似乎它不起作用,因为 in:

function getName(Object, name){
          return object.name;
    }

name 是一个变量。 在JS中,有没有办法将属性作为变量访问?

I have two objects:

object1={
   type: 'obj1',
   nName: 'nName'
}

object2={
   type: 'obj2',
   pName: 'pName'
}

In my js code, I have:

object=GET_OBJECT();

The GET_OBJECT() method returns either object1 or object2, then, I would like to access the object's name attribute which is either nName or pName.

I have one method which will get the name (pName or nName) of the returned object:

function getName(Object, name){
      return object.name;
}

where I would like the name to be a variable, so that I can access the pName or nName in this way:

object=GET_OBJECT();

var name='';

if(object.type=='obj1')
   name='nName';
else
   name='pName';

var finalName=getName(object, name);

But seems it won't work since in:

function getName(Object, name){
          return object.name;
    }

name is a variable. In JS, is there any way to access attribute as a variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-11-10 02:55:16

尝试这样:

function getName(Object, name) {
    return Object[name];
}

Try like this:

function getName(Object, name) {
    return Object[name];
}
梦过后 2024-11-10 02:55:16

正如之前很多次我想知道为什么人们提供解决方案而不是知识。否则提问者会一遍又一遍地重复同样的错误。

原始代码使用函数来检索属性。假设可以使用该参数来调用该参数。用更专业的话说,正在使用点表示法,它必须是有效的 JavaScript 标识符。点后面的名称是内容的指针。因此 getName 总是访问可能未定义的属性名称。

显示的解决方案使用括号表示法,其中使用参数的内容(可能不存在)作为标识符,然后解析内容,这就是它实际工作的原因。

点符号更快、更容易阅读,如果两者都是有效选项,我们会建议您使用点符号。当您需要在运行时解析时,可以使用括号表示法。这就是当您在内部定义 setter 和 getter 时会发生的情况。以下代码将使用传递的字符串来确保(使用括号表示法)每当您使用点表示法的标识符时都会调用传递的函数。

function myClass(){
        //setX and getX should be also defined previous to this
        this.__defineSetter__("x",this.setX);    
        this.__defineGetter__("x",this.getX); 
        //Object.defineProperty can also be used
};

var tObject = new myClass();
tObject.x = 10; //Getter being called

AS many times before I wonder why people provide solutions and not knowledge. Otherwise the asker will repeat the same mistakes over and over.

The original code uses a function to retrieve an attribute. It is assumed that it is possible to use the parameter to invoke the parameter. In more technical words, Dot notation is being used which must be a valid JavaScript identifier. Tha name after the dot is the pointer the content. Therefore getName always is accessing the attribute name which is likely to be undefined.

The solution shown uses Bracket notation in which uses the content of the parameter (which may not exist) as identifier and then, it resolves the content and this is why it actually works.

Dot notation is faster and easier to read and would be adviced if both are valid options. Bracket notation is to be used when you need resolving in run time. That is what happens when you define setters and getters internally. The following code will use the string passed to ensure (using bracket notation) that whenever you use the identifier by dot notation will call the functions passed.

function myClass(){
        //setX and getX should be also defined previous to this
        this.__defineSetter__("x",this.setX);    
        this.__defineGetter__("x",this.getX); 
        //Object.defineProperty can also be used
};

var tObject = new myClass();
tObject.x = 10; //Getter being called
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文