/*
Computed Property Names is ES6 feature which allows
the names of object properties in JavaScript OBJECT LITERAL NOTATION
to be determined dynamically, i.e. computed.
*/
let propertyname = 'c';
let obj ={
a : 11,
b : 12,
[propertyname] : 13
};
obj; // result is {a:11 , b:12 , c:13}
//or incase if you want a as your object you can set in this way
let a_value = {
[obj.a] = obj // a_value's key name as (a) and the complete (obj) present above itself will act as a value
};
let propName = 'c';
const rank = {
a: 1,
b: 2,
[propName]: 3,
};
console.log(rank.c); // 3
Code language: JavaScript (javascript)
let name = 'fullName';
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get [name]() {
return `${this.firstName} ${this.lastName}`;
}
}
let person = new Person('John', 'Doe');
console.log(person.fullName);
Code language: JavaScript (javascript)