JavaScript create objects, object inheritance practical ways to explain the basics _

Source: Internet
Author: User

This article convention: in the case of a special declaration, an attribute is referred to as a property or method.

Creating objects, object inheritance is actually one thing: the instance object we need obtains the private property through the constructor, and obtains the shared property through the prototype chain. What is the good way? Private properties are obtained by constructors (regardless of the custom private property in the instance) and do not need to be overridden, and shared properties are found through the prototype chain and do not need to be created repeatedly.

A pervasive approach

To create an object by combining the constructor pattern with the prototype schema

function Hnu_student (name) {
  this.name = name;
  This.sayname = function () {return
    this.name;}
  ;
} Hnu_student.prototype = {
  School: ' Hnu ',
  sayschool:function () {return
    this.school;
  }
};
Object.defineproperty (Hnu_student, ' constructor ', {value:hnu_student});

var Hiyohoo = new Hnu_student (' Xujian ');

Prototype is rewritten by literal means, and the constructor of the prototype points to object, where necessary to redefine constructor.

Parasitic Modular Inheritance

function Object (o) {
  function F () {};
  F.prototype = O;
  return new F ();
}
function Inheritprototype (Child, parent) {
  var prototype = object (Parent.prototype);
  Prototype.constructor = child;
  Child.prototype = prototype;
}

function Hnu_student (name) {
  this.name = name;
  This.sayname = function () {return
    this.name;}
  ;
} HNU_student.prototype.school = ' Hnu ';
HNU_student.prototype.saySchool = function () {return
  this.school;
};

function student_2011 (name, number) {
  Hnu_student.call (this, name);
  This.number = number;
  This.saynumber = function () {return
    this.number;
  }
}
Inheritprototype (student_2011, hnu_student);
Student_2011.prototype.graduationTime = 2015;
Student_2011.prototype.sayGraduationTime = function () {return
  this.graduationtime;
};

var Hiyohoo = new student_2011 (' Xujian ', 20110803203);

The role of object () is to convert the objects passed in as parameters into the prototype of the instance, which is shared by all instances.

Shared properties: Inheritprototype (student_2011, hnu_student); The Sub constructor prototype becomes an instance of the superclass, and attributes in the superclass stereotype are shared to the child constructors.
Private property: Hnu_student.call (this, name), which calls a super constructor to create a private property when an instance is created from a child constructor.

Other ways to create objects

Dynamic Prototyping Mode

function Hnu_student (name) {
  this.name = name;
  This.sayname = function () {return
    this.name;
  };

  if (! HNU_student.prototype.school) {
    HNU_student.prototype.school = ' Hnu ';
    HNU_student.prototype.saySchool = function () {return
      this.school;}
    ;

}} var Hiyohoo = new Hnu_student (' Xujian ');

Put the shared property defined in the stereotype into the constructor, and use the judgment statement to initialize the prototype share property when the first call to the constructor creates an instance.

Parasitic constructor patterns

function Specialarray () {
  var values = new Array ();
  Values.push.apply (values, arguments);
  values.topipedstring = function () {return
    this.join (' | ');

  return values;
}

var colors = new Specialarray (' Red ', ' black ', ' white ');

Used to add special properties to the native constructor.

Other ways that object inherits

Combined inheritance

function Hnu_student (name) {
  this.name = name;
  This.sayname = function () {return
    this.name;}
  ;
} HNU_student.prototype.school = ' Hnu ';
HNU_student.prototype.saySchool = function () {return
  this.school;
};
function student_2011 (name, number) {
  Hnu_student.call (this, name);
  This.number = number;
  This.saynumber = function () {return
    this.number;}
  ;
} Student_2011.prototype = new Hnu_student ();
Student_2011.prototype.constructor = student_2011;
Student_2011.prototype.graduationTime = 2015;
Student_2011.prototype.sayGraduationTime = function () {return
  this.graduationtime;
}
var Hiyohoo = new student_2011 (' Xujian ', 20110803203);

Shared properties: Student_2011.prototype = new Hnu_student (); The stereotype of the child constructor points to the prototype of the superclass, where the instance finds all the shared properties through the prototype chain.
Private property: Hnu_student.call (this, name), which calls a super constructor to create a private property when an instance is created from a child constructor.

Flaw: The super constructor was called two times. Student_2011.prototype = new Hnu_student (), the private property of the superclass definition is created in the child constructor prototype, and the private property in the stereotype is masked by the same attribute in the instance.

Prototype inheritance, parasitic inheritance

function Object (o) {
  function F () {}
  f.prototype = O;
  return new F ();
}
var student1 = {
  School: ' Hnu ',
  sayschool:function () {return
    this.school;
  }
};
var Student2 = object (student1);

Object.creat () is a new method of ECMAScript5 that accepts two parameters: one is the original object of the prototype, and the other is the object of the overridden or newly added property, which is the same as the custom object ().

var student1 = {
  name: ' Xujian ',
  School: ' Hnu '
};
var student2 = object.create (student1, {
  name: {
    value: ' Huangjing '
  }
});

Parasitic inheritance adds additional attributes to enhance an object based on a prototype inheritance.

function Object (o) {
  function F () {}
  f.prototype = O;
  return new F ();
}
function Creatanother (original) {
  var clone = object (original);
  Clone.sayhi = function () {
    alert (' hi! ');
  return clone;
}
var student1 = {
  School: ' Hnu ',
  sayschool:function () {return
    this.school;
  }
};
var student2 = Creatanother (student1);

Stereotype inheritance and parasitic inheritance are used to create instance objects that are similar to existing objects.

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.