Enhanced Object Literals
محتوى الدرس
main.js
/*
Enhanced Object Literals
1- Property Value Shorthand
2- Methods Shorthand
3- Computed Object Property Name
*/
// Before Property Value Shorthand
const username = "Osama",
website = "Elzero.org";
const user = {
username: username,
website: website
}
console.log(user)
console.log(user.username)
console.log(user.website)
// After Property Value Shorthand
const username = "Osama",
website = "Elzero.org";
const user = {
username,
website
}
console.log(user)
console.log(user.username)
console.log(user.website)
// Before Methods Shorthand
const user = {
myMethod: function (param1) {
return param1;
}
}
console.log(user.myMethod('Hello From Parameter'));
// After Methods Shorthand
const user = {
myMethod (param1) {
return param1;
}
}
console.log(user.myMethod('Hello From Parameter'));
// Before Computed Object Property Name
const myVariable = 'Key'; // Create Normal Variable
const myObject = { myProperty: 'MyValue' }; // Create New Object
myObject[myVariable] = 'Value'; // Add Dynamic Property and Assign A Value
console.log(myObject);
// After Computed Object Property Name
const myVariable = 'Key'; // Create Normal Variable
const myObject = { // Create New Object
myProperty: 'MyValue',
[myVariable]: 'Value'
};
console.log(myObject);