How to set an object function equal to another [duplicate]

  • A+
Category:Languages

This question already has an answer here:

Assuming I have the following object:

let obj={     childone: (value) => {         return value+1;     },     childtwo: (value) => {         return value+3;     },     childsingle: (value) => {         return value+1;     } }; 

Is there any method to set obj.childsingle equal to obj.childone within the same declaration?

I'm trying to achieve childsingle=childone within the object declaration.

I also tried to use get as per duplicated suggested answers.

However, after using this:

let obj = {     childone: (value) => {         return value+1;     },     childtwo: (value) => {         return value+3;     },     get childsingle() {         return this.childone;     } }; 

I get handleError TypeError: childsingle is not a function.

 


You can use getter method for childsingle declaration. Example is shown below.

Here in it will always return childone when you call for childsingle even if you update childone childone will always point to latest childone.

For more information refer Defining getters and setters Section

let obj = {     childone: (value) => {         return value+1;     },     childtwo: (value) => {         return value+3;     },     get childsingle() {         return this.childone;     } };  console.log(obj.childsingle(10));

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: