简体   繁体   English

javascript对象或数组? 这是什么?

[英]javascript objects or arrays? what is this?

function(property, res, err, result){
    var json = {};
    json[property] = result;
    res.json(json);
};

Okay, this is a function that will take the above parameters. 好的,此函数将采用上述参数。 When invoked it creates an Object called json, my question is about the next line I dont understand it at all, is the object then the list of properties? 当调用它时,它会创建一个名为json的对象,我的问题是我完全不理解的下一行,是对象,然后是属性列表? Please enlighten me. 请赐教。

There are 2 ways to set the properties on objects. 有两种方法可以设置对象的属性。 Most times people use dot notation like this: 大多数时候,人们使用点符号是这样的:

json.property = result;

But if the property name is a string (which is what will be passed in as the property argument), the way the object property is set is like this: 但是,如果属性名称是字符串(将作为property参数传递的字符串),则设置对象属性的方式如下:

json[property] = result

For example if someone puts these arguments into the function ("name", blah, blah, "Sam"), what is actually happening in the line in question is: 例如,如果有人将这些参数放入函数(“名称”,等等,等等,“山姆”)中,则该行中实际发生的是:

json["name"] = "Sam"

Which is equivalent to: 等效于:

json.name = "Sam"

And results in an object named json that looks like this: 并生成一个名为json的对象,如下所示:

{
  name: "Sam"
}

You are just setting the property of the object. 您只是在设置对象的属性。 Object properties can be accessed/set in different ways: 可以通过不同方式访问/设置对象属性:

objectName.property          // person.age

or 要么

objectName["property"]       // person["age"]

or, your case: 或者,您的情况:

objectName[expression]       // x = "age"; person[x]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM