(术语)如何引用变量中的变量? [重复]

时间:2021-04-23 16:48:10

This question already has an answer here:

这个问题在这里已有答案:

I need to clarify some terminology, because right now my project has a bit of a mix of terminology when refering to variables.

我需要澄清一些术语,因为现在我的项目在引用变量时有一些混合的术语。

Consider an object defined like this:

考虑一个像这样定义的对象:

var anObject = {
    a: {
        value1: 1337,
        value2: 69,
        value3: "420 man"
    }
}

Please correct me if I'm wrong, but I assume a is a property of the object anObject.

如果我错了,请纠正我,但我假设a是对象anObject的属性。

But in the context of anObject, how should I refer to value1? Is it a "property" aswell?

但是在anObject的上下文中,我应该如何引用value1?这是一个“财产”吗?

The reason I'm asking is I need to create functions to access "variables within variables" of objects. Like such:

我问的原因是我需要创建函数来访问对象中的“变量中的变量”。像这样:

function getProperty(name) {
    // ...
}

var theValueImLookingFor = getProperty("a.value1");

If this questions is inappropriate for Stack Overflow, please let me know where I should as this question instead. Thanks

如果这个问题不适合Stack Overflow,请告诉我这个问题应该放在哪里。谢谢

EDIT: I'm not asking how to access the nested variable, I'm asking how to refer to it in terminology.

编辑:我不是问如何访问嵌套变量,我问的是如何在术语中引用它。

Given that value1 is a property of a which is a property of anObject. What is value1 to anObject? Is it a "property-property"? I don't mean to sound flippant but what do I call it?

鉴于value1是a的属性,它是anObject的属性。什么是anObject的value1?它是“财产”吗?我不是故意轻率,但我称之为什么?

1 个解决方案

#1


2  

In the same way that a is a property of anObject, value1 is a property of a.

与a是anObject的属性的方式相同,value1是a的属性。

So you can access it by using:

所以你可以使用以下方法访问它:

anObject.a.value1

That's it. The long answer is, that this can be decomposed into the following code:

而已。答案很长,可以将其分解为以下代码:

const innerObject = anObject.a;
const result = innerObject.value1;

Please note that in JavaScript an object is nothing but a list of key-value pairs, i.e. a dictionary. You can use any data type as value, even another object, so you end up with nested objects - and this is exactly what you have here.

请注意,在JavaScript中,对象只是键值对的列表,即字典。您可以将任何数据类型用作值,甚至是另一个对象,因此您最终会得到嵌套对象 - 这正是您在此处所拥有的。

So for the function you are talking about, all you need to do is to split the given string by its separator char, such as a ., and then recursively walk the object tree until you get where you want to.

因此,对于您正在讨论的函数,您需要做的就是通过其分隔符char(例如。)拆分给定的字符串,然后递归地遍历对象树,直到找到您想要的位置。

Please note that e.g. on npm there are already ready-made modules that do exactly this, e.g. nested-keys (just to name one arbitrarily, there are lots of other modules that do the same or at least a similar task).

请注意,例如在npm上已经存在现成的模块,例如,嵌套键(只是为了任意命名,有许多其他模块执行相同或至少类似的任务)。

Even very common used libraries such as Lodash provide this functionality, so there is a good chance that you do not need to code it for yourself anyway, but can get away with using such a ready-made function. E.g., you might use [get](https://lodash.com/docs/4.16.6#get] of Lodash:

即使是非常常用的库,例如Lodash,也提供了这种功能,因此很有可能您无需为自己编写代码,但可以使用这样的现成功能。例如,您可以使用Lodash的[get](https://lodash.com/docs/4.16.6#get):

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// => 3

_.get(object, ['a', '0', 'b', 'c']);
// => 3

_.get(object, 'a.b.c', 'default');
// => 'default'

(This sample code is taken from the linked Lodash documentation on get.)

(此示例代码取自关于get的链接Lodash文档。)

If you still want to create your very own implementation, having a look at Lodash's source code may be a good starting point.

如果您仍想创建自己的实现,那么查看Lodash的源代码可能是一个很好的起点。

UPDATE

Regarding your edit, I don't think that there is an official term for this. You could call it a nested property, or a property of a sub-object. I think I'd rather describe as you did originally: value1 is a property of an object that is a property of anObject. That's not elegant, but technically correct, and as I said, I don't think there is a better (official) term.

关于你的编辑,我认为没有正式的术语。您可以将其称为嵌套属性或子对象的属性。我想我最初会像你原来那样描述:value1是一个对象的属性,它是anObject的一个属性。这不是优雅,但在技术上是正确的,正如我所说,我不认为有更好的(官方)术语。

#1


2  

In the same way that a is a property of anObject, value1 is a property of a.

与a是anObject的属性的方式相同,value1是a的属性。

So you can access it by using:

所以你可以使用以下方法访问它:

anObject.a.value1

That's it. The long answer is, that this can be decomposed into the following code:

而已。答案很长,可以将其分解为以下代码:

const innerObject = anObject.a;
const result = innerObject.value1;

Please note that in JavaScript an object is nothing but a list of key-value pairs, i.e. a dictionary. You can use any data type as value, even another object, so you end up with nested objects - and this is exactly what you have here.

请注意,在JavaScript中,对象只是键值对的列表,即字典。您可以将任何数据类型用作值,甚至是另一个对象,因此您最终会得到嵌套对象 - 这正是您在此处所拥有的。

So for the function you are talking about, all you need to do is to split the given string by its separator char, such as a ., and then recursively walk the object tree until you get where you want to.

因此,对于您正在讨论的函数,您需要做的就是通过其分隔符char(例如。)拆分给定的字符串,然后递归地遍历对象树,直到找到您想要的位置。

Please note that e.g. on npm there are already ready-made modules that do exactly this, e.g. nested-keys (just to name one arbitrarily, there are lots of other modules that do the same or at least a similar task).

请注意,例如在npm上已经存在现成的模块,例如,嵌套键(只是为了任意命名,有许多其他模块执行相同或至少类似的任务)。

Even very common used libraries such as Lodash provide this functionality, so there is a good chance that you do not need to code it for yourself anyway, but can get away with using such a ready-made function. E.g., you might use [get](https://lodash.com/docs/4.16.6#get] of Lodash:

即使是非常常用的库,例如Lodash,也提供了这种功能,因此很有可能您无需为自己编写代码,但可以使用这样的现成功能。例如,您可以使用Lodash的[get](https://lodash.com/docs/4.16.6#get):

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// => 3

_.get(object, ['a', '0', 'b', 'c']);
// => 3

_.get(object, 'a.b.c', 'default');
// => 'default'

(This sample code is taken from the linked Lodash documentation on get.)

(此示例代码取自关于get的链接Lodash文档。)

If you still want to create your very own implementation, having a look at Lodash's source code may be a good starting point.

如果您仍想创建自己的实现,那么查看Lodash的源代码可能是一个很好的起点。

UPDATE

Regarding your edit, I don't think that there is an official term for this. You could call it a nested property, or a property of a sub-object. I think I'd rather describe as you did originally: value1 is a property of an object that is a property of anObject. That's not elegant, but technically correct, and as I said, I don't think there is a better (official) term.

关于你的编辑,我认为没有正式的术语。您可以将其称为嵌套属性或子对象的属性。我想我最初会像你原来那样描述:value1是一个对象的属性,它是anObject的一个属性。这不是优雅,但在技术上是正确的,正如我所说,我不认为有更好的(官方)术语。