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.

jkdev
  • 11,360
  • 15
  • 54
  • 77
Vixed
  • 3,429
  • 5
  • 37
  • 68

3 Answers3

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));
Karan
  • 12,059
  • 3
  • 24
  • 40
  • 1
    Nice, but I got an error in console... *handleError TypeError: childsingle is not a function*. – Vixed Nov 29 '18 at 11:35
  • @Vixed Are you using `Internet Explorer`? Check browser compatibility here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get#Browser_compatibility – Karan Nov 29 '18 at 11:37
  • No Karam... Chrome... – Vixed Nov 29 '18 at 11:40
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));
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • @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 Nov 29 '18 at 09:35
  • @Vixed the top example does that - the function was just to show lexical scoping is preserved if you pass the object around. – Fenton Nov 29 '18 at 09:43
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

yunzen
  • 32,854
  • 11
  • 73
  • 106
  • `let obj={ childone: (value) => { return value+1; }, childtwo: (value) => { return value+3; }, childsingle: (value) => { return obj.childone(value) } };` also works – mplungjan Nov 29 '18 at 09:24
  • @mplungjan I saw this as comment on the OP. This does not apply as the OP states – yunzen Nov 29 '18 at 09:25
  • 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 – mplungjan Nov 29 '18 at 09:26
  • It **is** valid. It still does not apply to the OP – yunzen Nov 29 '18 at 09:29