Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

computed property in javascript

/*
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
};
Comment

Computed Property

let propName = 'c';

const rank = {
  a: 1,
  b: 2,
  [propName]: 3,
};

console.log(rank.c); // 3
Code language: JavaScript (javascript)
Comment

Computed Property

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)
Comment

PREVIOUS NEXT
Code Example
Javascript :: js loop through array 
Javascript :: convert string to array with condition javascirpt 
Javascript :: Promise.prototype.finally 
Javascript :: word table to json 
Javascript :: How To Use Multiple Styles in REACT 
Javascript :: how to add a property to a class in javascript 
Javascript :: template strings in js 
Javascript :: javascript date range 
Javascript :: pass props from child to parent 
Javascript :: remove duplicates strig javascript 
Javascript :: split javascript 
Javascript :: mongoose create text index to search for text 
Javascript :: ternary javascript 
Javascript :: javascript set query parameters in url 
Javascript :: anonymous functions in javascript 
Javascript :: Angular passing function as component input 
Javascript :: js concate map 
Javascript :: what is react js 
Javascript :: Set an onclick function with a parameter for an element 
Javascript :: useeffect cleanup function 
Javascript :: nextjs amp 
Javascript :: how to call a function javascript 
Javascript :: inertia js 
Javascript :: remix js 
Javascript :: javascript code for find the last element in array 
Javascript :: function statement js 
Javascript :: lettre au hasard javascript 
Javascript :: function in js 
Javascript :: google app script 
Javascript :: timer js 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =