Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 327
Vixed Avatar asked Nov 29 '18 08:11

Vixed


People also ask

Can you copy an object with object assign?

The Object. assign() method can be used to merge two objects and copy the result to a new target. Just like the spread operator, If the source objects have the same property name, the latter object will replace the preceding object. Now, let's look at another example of merging in Typescript.

How do you assign values from one object to another in TypeScript?

To use the Object. assign() method in TypeScript, pass a target object as the first parameter to the method and one or more source objects, e.g. const result = Object. assign({}, obj1, obj2) . The method will copy the properties from the source objects to the target object.


2 Answers

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));
like image 176
Karan Avatar answered Sep 19 '22 17:09

Karan


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));
like image 30
Fenton Avatar answered Sep 20 '22 17:09

Fenton