DEV Community

Dharan Ganesan
Dharan Ganesan

Posted on

Day 5: new, Object.create, and Object.assign

Object.assign:

It is a method used to copy the values of all enumerable own properties from one or more source objects to a target object. It performs a shallow copy, meaning that nested objects or arrays are copied by reference rather than creating new instances. The target object is returned after the properties have been assigned to it.

const target = { a: 1 };
const source = { b: 2 };
const result = Object.assign(target, source);
console.log(result); // { a: 1, b: 2 }
Enter fullscreen mode Exit fullscreen mode

Object.create:

It is a method used to create a new object with a specified prototype object and optional properties. The newly created object inherits properties from the prototype object. It allows you to establish a prototype chain, where the created object can access properties and methods from its prototype.

const person = {
  sayHello() {
    console.log('Hello!');
  }
};

const john = Object.create(person);
john.sayHello(); // Hello!
Enter fullscreen mode Exit fullscreen mode

new operator:

It is used to create an instance of an object by calling a constructor function with the new keyword. The new operator creates a new object, sets the prototype of that object to the constructor function's prototype, and executes the constructor function within the context of the newly created object. It allows the created object to inherit properties and methods from the constructor function's prototype.

function Person(name) {
  this.name = name;
}

Person.prototype.sayHello = function() {
  console.log('Hello, my name is ' + this.name);
};

const john = new Person('John');
john.sayHello(); // Hello, my name is John

Enter fullscreen mode Exit fullscreen mode

Object.assign/Object.create

These methods are not used to create instances of objects with constructors. Instead, they are used to copy properties from one object to another or create new objects with a specific prototype. They do not involve executing a constructor function or establishing a constructor-prototype relationship.

Polyfill for new:

if (!Object.new) {
  Object.new = function (constructor, ...args) {
    const obj = Object.create(constructor.prototype);
    const result = constructor.apply(obj, args);
    return result !== null && typeof result === 'object' ? result : obj;
  };
}

Enter fullscreen mode Exit fullscreen mode

Polyfill for Object.create:

if (!Object.create) {
  Object.create = function (proto, propertiesObject) {
    if (typeof proto !== 'object' && typeof proto !== 'function') {
      throw new TypeError('Object prototype may only be an Object or null');
    }
    function F() {}
    F.prototype = proto;
    const obj = new F();
    if (propertiesObject !== undefined) {
      Object.defineProperties(obj, propertiesObject);
    }
    return obj;
  };
}
Enter fullscreen mode Exit fullscreen mode

Polyfill for Object.assign:

if (!Object.assign) {
  Object.assign = function (target, ...sources) {
    if (target === null || target === undefined) {
      throw new TypeError('Cannot convert undefined or null to object');
    }
    const to = Object(target);
    sources.forEach(source => {
      if (source !== null && source !== undefined) {
        for (const key in source) {
          if (Object.prototype.hasOwnProperty.call(source, key)) {
            to[key] = source[key];
          }
        }
      }
    });
    return to;
  };
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)