Added a dot notation example
This commit is contained in:
parent
34b51dc7ec
commit
6c7c7d38ca
1 changed files with 12 additions and 2 deletions
|
@ -105,7 +105,6 @@ That's because the dot requires the key to be a valid variable identifier. That
|
|||
|
||||
There's an alternative "square bracket notation" that works with any string:
|
||||
|
||||
|
||||
```js run
|
||||
let user = {};
|
||||
|
||||
|
@ -130,7 +129,7 @@ let key = "likes birds";
|
|||
user[key] = true;
|
||||
```
|
||||
|
||||
Here, the variable `key` may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility. The dot notation cannot be used in a similar way.
|
||||
Here, the variable `key` may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility.
|
||||
|
||||
For instance:
|
||||
|
||||
|
@ -146,6 +145,17 @@ let key = prompt("What do you want to know about the user?", "name");
|
|||
alert( user[key] ); // John (if enter "name")
|
||||
```
|
||||
|
||||
The dot notation cannot be used in a similar way.
|
||||
|
||||
```js run
|
||||
let user = {
|
||||
name: "John",
|
||||
age: 30
|
||||
};
|
||||
|
||||
let key = "name";
|
||||
user.key // undefined
|
||||
```
|
||||
|
||||
### Computed properties
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue