Object assign - amorfati0310/javascript-vm GitHub Wiki

Object-assign

Object assign๋ฉ”์†Œ๋“œ ์ •๋ฆฌ๊ฐ€ ์ž˜ ๋˜์–ด ์žˆ๋Š” Page

  1. Constructor์—์„œ ์‚ฌ์šฉ
Class Point {
 constructor(x,y){
  this.x = x;
  this.y = y;
  }
}

   class Point {
        constructor(x, y) {
            Object.assign(this, { x, y });
        }
    }
์ด๋Ÿฐ ๋ถ€๋ถ„์„ constructor(Object.assign(this, {x,y})๋กœ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

2.prototype Pattern

   MyClass.prototype.foo = function (arg1, arg2) {
        ...
    };


   Object.assign(MyClass.prototype, {
        foo(arg1, arg2) {
            ...
        }
    });


  1. copy obj
Object.assign({ __proto__: obj.__proto__ }, obj);
Object.assign({}, obj) only ownProperty copy