Object.create method and its application in inheritance

Source: Internet
Author: User
Method Introduction

The Object.create method is a method for creating objects in JavaScript.
Object.create receives two parameters, the first represents the object to inherit, and the second parameter represents an object that is used to initialize the newly created object. Here we look at the specific application:

var Obj ={
 Name: ' Mini ',
 Age:3,
 show:function () {
     Console.log (this.name + "is" +this.age);
 }

The Obj object above is an original object that is used for inheritance.

var myobj = object.create (obj,{
like:{
 value: "Fish",   //Initialize assignment
 writable:true,//   whether or not to be rewritten
 Configurable:true,//whether can be deleted, whether can be modified
 enumerable:true    //Whether can be used for enumeration
},
hate:{
Configurable:true,
get:function () {console.log ();  Return ' mouse '},  /////Get the method console.log that is triggered when the object hate property is
set:function (value) {  //Setting the Object Hate property 
    ( value,2222);
    return value;
}}}
);

Note: The property of the second parameter of the Object.create method is also an object, and all properties in the object are fixed, not custom. What each attribute represents, we have marked in the comment on the top code. We can deepen our understanding of this approach through the following code:

Console.log (myobj.name);  Mini
Console.log (myobj.like);  Fish
myobj.like = "peanut";    Property modifies the assignment
Console.log (myobj.like);  Peanut
myobj.hate;  The trigger Get    method
myobj.hate = ' dog ';///dog,2222   Trigger Set method

I believe you can probably understand the use of the Object.create method by the result of the above execution. And you may also notice that there seems to be a greater potential for get and set methods. We can use them to implement data filtering and data binding. To achieve some simple mvvm effect. But that's not the point of our discussion. The application in inheritance

Before we explain the application of the Object.create method to the constructor, we will review the inheritance of the constructor function first.

var animal=function () {}
animal.prototype.show=function () {
    console.log ("Animal");
}
var  Dog = function () {}
var subclass = function () {}
subclass.prototype = Animal.prototype;
Dog.prototype = new Subclass (); Inherit animal 

The code above is one of the most common ways we can implement inheritance, by virtue of an agent function that inherits the properties on the constructor by inheriting the instance of the constructor.

After you understand the Object.create method, you may be able to implement inheritance in a different way:

var A = function () {};
A.prototype.sayname=function () {
    console.log (' a ');
}

The instance of B inherits the attribute of a of
var B = function () {};
B.prototype = Object.create (a.prototype);
var B = new B ();
B.sayname ();  A

Through detection, we find that inheritance can also be implemented in this way by inheriting the prototype object and assigning it to its own prototype object. But is this way the perfect inheritance we want?

Rewrite B's prototype
b.prototype= {
    sayname:function () {
        console.log ("B");
    }  after rewriting, the instance properties of B have changed
var b1 = new B ();
B1.sayname ();  The instance of B//A maintains the original attribute and has not been overwritten. To achieve the inheritance effect we want
var a1 = new A ();
A1.sayname (); A

Through the above case, we can find that object.create this way to achieve the perfect separation of the A,b prototype. The two sides do not interact with each other, which means that this is a truly viable way to implement inheritance.

However, the black-tech approach, like object.create, generally has compatibility problems. So we also need to deal with compatibility issues in order to be assured of use.

if (typeof object.create!= "function")
 {
 object.create=function (o) {
 function F () {}
 F.prototype =o;
 return new F ();
 }
 }

The

Through the patch code above seems to go back to the way we handled inheritance before, so that we can implement inheritance in all browsers through B.prototype =object.create (a.prototype).

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.