简体   繁体   English

点和方括号表示法

[英]Dot and Square Bracket Notation

I'm trying to understand the difference between the Dot and the Square Bracket Notation.我试图了解点和方括号表示法之间的区别。 While going through various examples here on SO and on some other sites, I came across these two simple examples:在 SO 和其他一些网站上查看各种示例时,我遇到了以下两个简单示例:

 var obj = { "abc" : "hello" }; var x = "abc"; var y = obj[x]; console.log(y); //output - hello

 var user = { name: "John Doe", age: 30 }; var key = prompt("Enter the property to modify","name or age"); var value = prompt("Enter new value for " + key); user[key] = value; alert("New " + key + ": " + user[key]);

The first example returns y to be undefined if in third line I replace the obj[x] with obj.x .如果在第三行中我将obj[x]替换为obj.x ,则第一个示例返回 y 未定义。 Why not "hello"为什么不是"hello"


But in the second example the expression user[key] can simply be replaced with user.key without any anomalous behavior (at-least to me).但是在第二个示例中,表达式user[key]可以简单地替换为user.key没有任何异常行为(至少对我而言)。 Now this confuses me as I recently learned that if we want to access properties by name stored in a variable, we use the [ ] Square bracket Notation.现在这让我很困惑,因为我最近了解到,如果我们想通过存储在变量中的名称访问属性,我们使用 [ ] 方括号表示法。

In dot notation, the name after the dot is the name of the property being referenced.在点表示法中,点后面的名称是被引用的属性的名称。 So:所以:

var foo = "bar";
var obj = { foo: 1, bar: 2 };

console.log(obj.foo) // = 1, since the "foo" property of obj is 1,
                     //      independent of the variable foo

However, in square-bracket notation, the name of the property being referenced is the value of whatever is in the square brackets:然而,在方括号表示法中,被引用的属性的名称是方括号中任何内容的

var foo = "bar";
var obj = { foo: 1, bar: 2 };

console.log(obj[foo])   // = 2, since the value of the variable foo is "bar" and
                        //      the "bar" property of obj is 2

console.log(obj["foo"]) // = 1, since the value of the literal "foo" is "foo" and
                        //      the "foo" property of obj is 1

In other words, dot-notation obj.foo is always equivalent to obj["foo"] , while obj[foo] depends on the value of the variable foo .换句话说,点符号obj.foo总是等价于obj["foo"] ,而obj[foo]取决于变量foo的值。


In the specific case of your question, note the differences between dot notation and square-bracket notation:在您的问题的特定情况下,请注意点表示法和方括号表示法之间的区别:

// with dot notation
var obj = { name: "John Doe", age: 30 };
var key = "age";
var value = 60;

obj.key = value; // referencing the literal property "key"
console.log(obj) // = { name: "John Doe", age: 30, key: 60 }


// with square bracket notation
var obj = { name: "John Doe", age: 30 };
var key = "age";
var value = 60;

obj[key] = value; // referencing property by the value of the key variable ("age")
console.log(obj)  // = { name: "John Doe", age: 60 }

Accessing/Creation of properties from/in a JavaScript object can be done in two ways从/在 JavaScript 对象中访问/创建属性可以通过两种方式完成

  1. Using Dot notation使用表示法
  2. Using Square Bracket notation使用方括号表示法

Whenever some property is not defined ie not present in the object and you try to access it, you will get undefined (obviously, because it's not there).每当某些属性未定义,即对象中不存在并且您尝试访问它时,您将获得undefined (显然,因为它不存在)。

So, in the first example you are accessing a property and in the second example you are creating a property.因此,在第一个示例中,您正在访问一个属性,而在第二个示例中,您正在创建一个属性。 Therefore, replacing the notation did not affect the code in the second example.因此,替换符号不会影响第二个示例中的代码。

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

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