Friday, 26 May 2017

How to make Object.assign work?

Background

I am trying to extend the functionality of an old object via Object.assign, by passing in a new object with an additional feature.

    const oldObj = () => { 
        const printLog = () => console.log("hello");
        return {printLog};
    };
    
    const newObj = () => {   
        const test = () => {
            printLog(); //fails here!
            console.log("world");
        };
        return {test}; 
    };
    
    const mix = Object.assign(oldObj(), newObj());
    
    mix.printLog();
    mix.test();

Problem

My mix object fails execution, even though it has bot printLog and test methods:

Object {printLog: function, test: function}

Question

How can I fix my code so that the test function will work as expected?



via Flame_Phoenix

No comments:

Post a Comment