How to use Object.assign() to add properties to this in constructor with JavaScript ES6

1 Answer

0 votes
class CTest {
    constructor(x, y) {
        Object.assign(this, {x, y});
    }
}

const obj = new CTest(45, 8768);

console.log(`${obj.x} ${obj.y}`);



 
/*
run:
      
45 8768
 
*/

 



answered Mar 26, 2020 by avibootz

Related questions

...