Literal Notation Extensions
There are some improvements to Literal Notation syntax.
For instance:
let width = 10;
let length = 50;
rectangle = {
"width": width,
"length": length,
"OutputSize": function OutputSize() {
console.log(this.width * this.length);
},
"Is It a Square": function IsItASquare() {
console.log(this.width == this.length);
}
};
Can now be written
let width = 10;
let length = 50;
let lengthField = 'length'; // Creation of a dynamic field that use square bracket `[]` for reference in the object below
rectangle = {
width, // width is automatically assigned - takes above value
[lengthField]: length, // Dynamic field name using variable. It takes both the property and value in this example
outputSize() { // shorter syntax
console.log(this.width * this.length); // this refers to object
},
"Is It a Square"() { possibility of string name, even accepting space inside the string
console.log(this.width == this.length);
}
};
PS: Short way of writing
outputSize: function outputSize() {...)
is:outputSize() {...)
More information may be found here: Click to read more