Esc6 object part 1

ES6 Objects



JavaScript variables can be Object data types that contain many values called properties
An object can also have properties that are function definitions called methods for performing actions on the object
ES6 introduces shorthand notations and computed property names that make declaring and using objects easier to understand.

The new method definition shorthand does not require the colon (:) or function keyword, as in the grow function of the tree objectdeclaration:
let tree = {
height: 10,
color: 'green',
grow() {
this.height += 2;
}
};
tree.grow();
console.log(tree.height); // 12
Try It Yourself

You can also use a property value shorthand when initializing properties with a variable by the same name. 
For example, properties height and health are being initialized with variables named height and health:
let height = 5;
let health = 100;

let athlete = {
height,
health
};
Try It Yourself

When creating an object by using duplicate property names, the last property will overwrite the prior ones of the same name.
For Example:
var a = {x: 1, x: 2, x: 3, x: 4};Try It Yourself

Duplicate property names generated a SyntaxError in ES5 when using strict mode. However, ES6 removed this limitation.

Comments