DEV Community

Dharan Ganesan
Dharan Ganesan

Posted on

1

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

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more