How to use Object.create ()

Source: Internet
Author: User

The Object.create () method creates a new object using the specified prototype object and its properties. Grammar

Object.create (proto[, Propertiesobject])
ParametersProto the prototype object for the newly created object. Propertiesobject Optional. If not specified as undefined, the property descriptor of the object to add to the newly created object (that is, its own defined property, not the enumeration property on its prototype chain), and the corresponding property name. These properties correspond to the second parameter of Object.defineproperties (). return value

The object after the new attribute is added to the specified prototype object. Exceptions

If the proto parameter is not null or an object, a TypeError exception is thrown. Example implementation of class inheritance with Object.create

The following example shows how to use Object.create () to implement class inheritance. This is a single inheritance supported by all versions of JavaScript.

//Shape-Parent class (superclass) function Shape () {this.x = 0;
This.y = 0;
  }//Parent class Method Shape.prototype.move = function (x, y) {this.x = x;
  This.y = y;
Console.info (' Shape moved. ');

};

Rectangle-Subclass (Subclass) function Rectangle () {shape.call (this);//Call Super constructor.}
Subclass Continuation Parent Class Rectangle.prototype = Object.create (Shape.prototype);

Rectangle.prototype.constructor = Rectangle; Because of the use of ". Prototype = ...", constructor will change to the one//constructor of "= ...", so to reassign. Constructor for itself. 
var rect = new Rectangle ();

Console.log (' is rect a instance of Rectangle? ',
  rect instanceof-Rectangle);//True
Console.log (' is rect a Insta nCE of shape? ',
  rect instanceof shape); True
rect.move (1, 1);//outputs, ' Shape moved. '

If you want to be able to inherit multiple objects, you can use the mixed method.

function MyClass () {
     superclass.call (this);
     Othersuperclass.call (this);
}

Inheriting a class
Myclass.prototype = Object.create (superclass.prototype);
Mixed
with other object.assign (Myclass.prototype, othersuperclass.prototype);
Re-designate constructor
MyClass.prototype.constructor = MyClass;

MyClass.prototype.myMethod = function () {
     //do a thing
};

/p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.