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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay