简体   繁体   English

Javascript _ 点符号和括号符号

[英]Javascript _ Dot notation and Bracket Notation

I have two questions as below.我有两个问题如下。 Could any one help to explain?谁能帮忙解释一下? If you have any material source, please also help to provide the link.如果有素材来源,也请帮忙提供链接。 Thank you!谢谢你!

Question 1: Why below 2 outputs are different.问题1:为什么下面2个输出不同。 One is 37. The other is 40. I thought both ouputs should be 40. Please help to explain why.一个是37。另一个是40。我认为两个输出都应该是40。请帮助解释原因。


let me = {
    firstName: 'Terry',
    gender: 'Male',
    age: 37
};

let selection = 'age';
me.selection = 40;

console.log('Bracket Notation of selection: ', me[selection]);  // output: 37
console.log('Dot Notation of selection: ', me.selection); // out put: 40

Question 2: This time, I change from dot notation (me.selection = 40) to bracket notation (me[selection] = 40).问题2:这次,我从点号(me.selection = 40)改为括号号(me[selection] = 40)。 The first output change from 37 to 40. The second output change from 40 to undefined.第一个 output 从 37 变为 40。第二个 output 从 40 变为未定义。 Please help to explain why.请帮忙解释原因。

let me = {
    firstName: 'Terry',
    gender: 'Male',
    age: 37
};

let selection = 'age'
me[selection] = 40; //change from dot notation (me.selection = 40) to bracket notation (me[selection] = 40)

console.log('Bracket Notation of selection: ', me[selection]);  // output: 40
console.log('Dot Notation of selection: ', me.selection); // output: undefined

Answer to question 1:问题1的答案:

me[selection] is equivalent to saying me["age"] ...which is 37 me[selection]等同于说me["age"] ...也就是 37

me.selection is equivalent to saying me["selection"] ... so answer is 40 me.selection相当于说me["selection"] ... 所以答案是 40

Answer to question 2:问题2的答案:

me[selection] = 40; is equivalent to saying me["age"] = 40相当于说me["age"] = 40

So when you do me.selection in console.log afterwards, it returns undefined because it can't find a selection key所以当你之后在 console.log 中执行me.selection时,它返回undefined因为它找不到selection

When you say me[selection] it returns 40 because you're really asking me["age"] ..and this time you're referring to the variable "selection" not the key selection当你说me[selection]它返回 40 因为你真的在问me["age"] .. 而这次你指的是变量“selection”而不是关键选择

Since selection is equal to 'age' me[selection] is equivalent to me['age'] (or me.age) which was 37 but was reassigned to 40由于 selection 等于 'age' me[selection] 等于 me['age'] (或 me.age),它是 37 但被重新分配为 40

me.selection is equivalent to me['selection'] which doesn't exist on that object me.selection 等同于 me['selection'] 不存在于 object

When you use the dot notation, you are giving JavaScript the name of the property directly, but when you use a variable inside a bracket notation to access a property value, JavaScript cares about the value of the variable, not the name of it .当您使用点表示法时,您是直接给 JavaScript 属性名称,但是当您使用括号表示法中的变量来访问属性值时, JavaScript 关心变量的值,而不是它的名称

the mechanism is simple机制很简单

dot notation: me.selection =>>>> js directly looks for property name "selection".点符号:me.selection =>>>> js直接查找属性名称“selection”。

bracket notation: me[selection] =>>> js looks for the value of selection, it equals "age", looks for property name age.括号表示法:me[selection] =>>> js 查找 selection 的值,它等于“age”,查找属性名称 age。

Where can this be useful?这在哪里有用? Sometimes you want to make properties based on some other constant or variable in your code有时您想根据代码中的一些其他常量或变量创建属性

Here you create properties based on a constant that equals to 10:在这里,您根据等于 10 的常量创建属性:

let neededProperties=10
for (let i=0;i<neededProperties;i++) {
    neededProperties["prop"+i]=i
}

Property accessors provide access to an object's properties by using the dot notation or the bracket notation.属性访问器使用点表示法或括号表示法提供对对象属性的访问。 In dot notation we have to give the exact propert for accessing its value and in bracket notation we pass the dyanmic value which have the property name to access the property value for the object.在点表示法中,我们必须给出访问其值的确切属性,在方括号表示法中,我们传递具有属性名称的动态值来访问 object 的属性值。

So, Explanation: you have an object named me with different properties and also a vaiable with selection Thus, will accessing the value Using dot notation ie me.selection you get the value form object property directly which is 40.所以,解释:你有一个名为 me 的 object,它具有不同的属性,还有一个带有选择的 vaiable 因此,将使用点符号访问值,即 me.selection 你直接从 object 属性中获得值,即 40。

Using bracket notation ie me[selection], in ths case the selection variable is used and this varibale will be replaced with its value like me['age'].使用方括号表示法,即 me[selection],在这种情况下,将使用选择变量,并且此变量将被替换为它的值,如 me['age']。 So, you get the value from object having property name age.因此,您从具有属性名称 age 的 object 中获取值。

Hope you understand the logic and reason for both, and will find the answers for both of your questions and also see details here希望您了解两者的逻辑和原因,并找到您的两个问题的答案,并在此处查看详细信息

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

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