object-create-x

Sham for Object.create


Keywords
Object, create, module, javascript, nodejs, browser, ecmascript
License
MIT
Install
npm install object-create-x@3.0.10

Documentation

Travis status Dependency status devDependency status npm version

object-create-x

Sham for Object.create

module.exportsboolean

This method method creates a new object with the specified prototype object and properties.

Kind: Exported member
Returns: boolean - A new object with the specified prototype object and properties.
Throws:

  • TypeError If the properties parameter isn't null or an object.
Param Type Description
prototype * The object which should be the prototype of the newly-created object.
[properties] * If specified and not undefined, an object whose enumerable own properties (that is, those properties defined upon itself and not enumerable properties along its prototype chain) specify property descriptors to be added to the newly-created object, with the corresponding property names.

Example

import create from 'object-create-x';

// Shape - superclass
function Shape() {
  this.x = 0;
  this.y = 0;
}

// superclass method
Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
  console.info('Shape moved.');
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); // call super constructor.
}

// subclass extends superclass
Rectangle.prototype = create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

const rect = new Rectangle();

console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?', rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'