Es6 Ecmascript Typescript Object Literals Examples
these are some shorthand notations with object literals is when you the property and the value are the same, you dont have to write them both:
this inside the curly braces {}
for example:let one=1;
let two=2;
let example = {
one:one,
two:two
}
YOU CAN CHANGE TO:let example = {
one,
two
}
ANOTHER WAY IS LIKE THIS:function foo(one,two,three){
let numbers = one+two
return{
one,
two,
numbers,
isNumber: function(){
return three>10 // returns true if tree is greater than 10
}
}
}
YOU CAN CHANG IT TO by simply removing the word function isNumber: (){
return three>10 // returns true if tree is greater than 10
}
in ES6 you can have spaces in your object property name
// EXAMPLE 1let person = {
"first name": "Tom"
};
console.log( person["first name"]);
// EXAMPLE 2
//YOU CAN ALSO CREATE VARIABLES AS PROPERTY NAMESlet ln = "last name";
let person = {
"first name": "Tom",
[ln] : "Smith"
};
console.log( person); // Object {first name: "Tom", last name: "Smith"}
I learned from this:
ES6 and Typescript Tutorial - 15 - Object Literals Part 1
https://www.youtube.com/watch?v=fgoNcYHxfdM&list=PLC3y8-rFHvwhI0V5mE9Vu6Nm-nap8EcjV&index=15
https://www.youtube.com/watch?v=L8JdlGYQGfw&index=16&list=PLC3y8-rFHvwhI0V5mE9Vu6Nm-nap8EcjV
ES6 and Typescript Tutorial - 16 - Object Literals Part 2