简体   繁体   English

如何访问JavaScript对象的属性?

[英]How to access the properties of a JavaScript object?

while review a javascript coding, i saw that 在查看javascript编码时,我看到了

var detailInf = {
  "hTitle":"Results",
  "hMark":"98"
};

What's the concept behind this js coding. 这个js编码背后的概念是什么。 While give alert for the variable its shows as "[object Object]". 对变量发出警报时,其显示为“ [object Object]”。 So this is an object, then how can we access the variable and reveal the data from this object. 所以这是一个对象,然后我们如何访问变量并显示该对象的数据。

Try doing this: 尝试这样做:

alert(detailInf['hTitle']);
alert(detailInf.hTitle);

Both will alert "Results" - this is a Javascript object that can be used as a dictionary of sorts. 两者都会警告“结果”-这是一个Javascript对象,可以用作字典。

Required reading: Objects as associative arrays 必读: 对象为关联数组

As a footnote, you should really get Firebug when messing around with Javascript. 作为一个脚注,您在弄乱Javascript时应该真的得到Firebug You could then just console.log(detailInf); 然后,您可以只是console.log(detailInf); and you would get a nicely mapped out display of the object in the console. 这样您就可以在控制台中很好地映射对象的显示。

That form of a JavaScript object is called an object literal , just like there are array literals. 就像存在数组文字一样,JavaScript对象的这种形式称为对象文字。 For example, the following two array declarations are identical: 例如,以下两个数组声明是相同的:

var a = [1, 2, 3];          // array literal
var b = new Array(1, 2, 3); // using the Array constructor

Just as above, an object may be declared in multiple ways. 就像上面一样,可以用多种方式声明一个对象。 One of them is object literal in which you declare the properties along with the object: 其中之一是对象文字,您可以在其中声明属性以及对象:

var o = {property: "value"}; // object literal

Is equivalent to: 等效于:

var o = new Object; // using the Object constructor
o.property = "value";

Objects may also be created from constructor functions. 对象也可以从构造函数创建。 Like so: 像这样:

var Foo = function() {
    this.property = "value";
};

var o = new Foo;

Adding methods 添加方式

As I said in a comment a few moments ago, this form of declaring a JavaScript object is not a JSON format. 正如我不久前在评论中所说,声明JavaScript对象的这种形式不是JSON格式。 JSON is a data format and does not allow functions as values. JSON是一种数据格式,不允许将函数用作值。 That means the following is a valid JavaScript object literal, but not a valid JSON format: 这意味着以下是有效的JavaScript对象文字,但不是有效的JSON格式:

var user = {
    age : 16,

    // this is a method
    isAdult : function() {
        // the object is referenced by the special variable: this
        return this.age >= 18;
    }
};

Also, the name of the properties need not be enclosed inside quotes. 同样,属性名称不必用引号引起来。 This is however required in JSON. 但是,这在JSON中是必需的。 In JavaScript we enclose them in brackets where the property name is a reserved word, like class , while and others. 在JavaScript中,我们将它们放在方括号中,其中属性名称是保留字,例如classwhile和其他。 So the following are also equivalent: 因此,以下内容也等效:

var o = {
    property : "value",
};

var o = {
    "property" : "value",
};

Further more, the keys may also be numbers: 此外,键也可以是数字:

var a = {
    0 : "foo",
    1 : "bar",
    2 : "abz"
};

alert(a[1]); // bar

Array-like objects 类数组对象

Now, if the above object would have also a length property, it will be an array like object: 现在,如果上面的对象也具有length属性,它将是一个类似于object的数组:

var arrayLike = {
    0 : "foo",
    1 : "bar",
    2 : "baz",

    length : 3
};

Array-like means it can be easily iterated with normal iteration constructs (for, while). 类数组意味着可以使用普通迭代构造(for,while)轻松地对其进行迭代。 However, you cannot apply array methods on it. 但是,不能在其上应用数组方法。 Like array.slice(). 就像array.slice()。 But this is another topic. 但这是另一个话题。

Square Bracket Notation 方括号符号

As Paolo Bergantino already said, you may access an object's properties using both the dot notation, as well as the square bracket notation. 正如Paolo Bergantino所说,您可以使用点符号和方括号符号来访问对象的属性。 For example: 例如:

var o = {
    property : "value"
};

o.property;
o["property"];

When would you want to use one over the other? 您什么时候要使用另一个? People use square bracket notation when the property names is dynamically determined, like so: 人们在动态确定属性名称时使用方括号表示法,如下所示:

var getProperty = function(object, property) {
    return object[property];
};

Or when the property name is a JavaScript reserved word, for example while . 或者,当属性名称是JavaScript保留字时,例如while

object["while"];
object.while; // error

That's an object in JSON format . 那是 JSON格式的对象 That's a javascript object literal . 那是一个javascript对象文字 Basically, the bits to the left of the : 's are the property names, and the bits to the right are the property values. 基本上, :的左边的位是属性名称,右边的​​位是属性值。 So, what you have there is a variable called detailInf , that has two properties, hTitle and hMark . 因此,您所拥有的是一个名为detailInf的变量,它具有两个属性hTitlehMark hTitle 's value is Results, hMark 's value is 98. hTitle的值是Results, hMark的值是98。

var detailInf = { "hTitle":"Results", "hMark":"98"};
alert(detailInf.hTitle); //should alert "Results"
alert(detailInf.hMark); //should alert "98

Edit Paolo's answer is better :-) 编辑 Paolo的答案更好:-)

As Dan F says, that is an object in JSON format. 正如Dan F所说,这是JSON格式的对象。 To loop through all the properties of an object you can do: 要遍历对象的所有属性,可以执行以下操作:

for (var i in foo) {
    alert('foo[' + i + ']: ' + foo[i]);
}

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

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