Last active
August 16, 2018 11:17
-
-
Save CodHeK/63aefc31dee133a1964129049ffe1beb to your computer and use it in GitHub Desktop.
obj literal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = "Gagan" | |
let age = 20 | |
let obj = { | |
name, //shorthand for name: name, | |
age, | |
} | |
console.log(obj.name) //prints "Gagan" | |
console.log(obj.age) //prints 20 | |
let name = "Gagan" | |
let age = 20 | |
let ageField = "age" | |
let obj = { | |
"name": "Max", | |
[ageField]: 20, //needs initialization | |
greet() { | |
console.log(this.name + " " + this.age) | |
} | |
} | |
console.log(obj["name"]) //prints "Max" | |
obj.greet() //prints "Max 20" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment