如何将对象的属性作为变量访问?
我有两个对象:
object1={
type: 'obj1',
nName: 'nName'
}
object2={
type: 'obj2',
pName: 'pName'
}
在我的 js 代码中,我有:
object=GET_OBJECT();
GET_OBJECT()
方法返回 object1 或 object2,然后,我想访问对象的 name 属性,该属性是 nName 或 pName。
我有一种方法可以获取返回对象的名称(pName 或 nName):
function getName(Object, name){
return object.name;
}
我希望 name
是一个变量,以便我可以访问 pName
或 nName
以这种方式:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样:
Try like this:
正如之前很多次我想知道为什么人们提供解决方案而不是知识。否则提问者会一遍又一遍地重复同样的错误。
原始代码使用函数来检索属性。假设可以使用该参数来调用该参数。用更专业的话说,正在使用点表示法,它必须是有效的 JavaScript 标识符。点后面的名称是内容的指针。因此 getName 总是访问可能未定义的属性名称。
显示的解决方案使用括号表示法,其中使用参数的内容(可能不存在)作为标识符,然后解析内容,这就是它实际工作的原因。
点符号更快、更容易阅读,如果两者都是有效选项,我们会建议您使用点符号。当您需要在运行时解析时,可以使用括号表示法。这就是当您在内部定义 setter 和 getter 时会发生的情况。以下代码将使用传递的字符串来确保(使用括号表示法)每当您使用点表示法的标识符时都会调用传递的函数。
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.