5

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.

8
  • You can define the function as a const before the object declaration and pass the same function to both of them Commented Nov 29, 2018 at 9:00
  • Within object literal - NO. Commented Nov 29, 2018 at 9:01
  • Isn't this related? stackoverflow.com/questions/4616202/… Commented Nov 29, 2018 at 9:02
  • yes @WiktorZychla but I cannot use this because is related to the class. Commented Nov 29, 2018 at 9:04
  • 2
    Don't use arrow function childsingle: function(value) {return this.childone(value)} Commented Nov 29, 2018 at 11:03

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Nice, but I got an error in console... handleError TypeError: childsingle is not a function.
@Vixed Are you using Internet Explorer? Check browser compatibility here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
No Karam... Chrome...
3

What is the reason to avoid:

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

It provides:

console.log(obj.childone(1)); // 2 console.log(obj.childsingle(2)); // 3 

Lexical scoping keeps in honest in more advanced scenarios too:

function getObj() { const obj = { childone: (value) => { return value + 1; }, childtwo: (value) => { return value + 3; }, childsingle: (value) => { return obj.childone(value); } }; return obj; } const example = getObj(); console.log(example.childone(1)); console.log(example.childsingle(2)); 

2 Comments

@Fentom +1 for the explanation, but I'm trying to set only an object fn equal to another, not to set a new object everytime I need it.
@Vixed the top example does that - the function was just to show lexical scoping is preserved if you pass the object around.
2

You might think of something like this:

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

But that doesn't work. You have to do it in two steps.

The way the assign operator (=) works, is as follows. It uses the right side of the operator, computes its value and assigns that value (which is a reference to an object in your case) to the variable on the left side.

So at the time the right hand side of the assign operator is assembled, the obj variable is not existent. So you will get an error.

This will also not work

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

because this will not refer to the current object.

This will not work, because childone is not defined

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

This is the way to go:

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

But beware, if you are planning to change the childone function and think that the childsingle function will change as well, that is not true.

Before changing childone, we have two keys of the object (childone and childsingle) that are references to the same function. But childone and childsingle are not related elsewise. If you change chilone this will be a reference to a new function and childsingle will remain the same old function

4 Comments

let obj={ childone: (value) => { return value+1; }, childtwo: (value) => { return value+3; }, childsingle: (value) => { return obj.childone(value) } }; also works
@mplungjan I saw this as comment on the OP. This does not apply as the OP states
I do not see why that is not as valid as obj.childsingle = obj.childone - with the additional opportunity to detect which one was called without duplicating the processing
It is valid. It still does not apply to the OP

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.