JS Object-oriented (ii)---inheritance

Source: Internet
Author: User
Tags ming

First, object-oriented inheritance

1, Analysis: On the basis of the original object, slightly modified to get a new object, and does not affect the original object function

2. How to add inheritance---copy inheritance

Properties: Call

Method: For in

/* Inheritance: Subclasses do not affect the parent class, subclasses can inherit some of the functionality of the parent class (code reuse) property inheritance: Call the parent class to construct the 1 function of the calling method's inheritance: For in   copy inherits *///parent class function Createperson (Name,sex) { this.name = Name;this.sex = sex;} CreatePerson.prototype.showName = function () {alert (this.name)}var p1 = new Createperson (' Xiao Ming ', ' Man ') p1.showname ()   / /xiaoming//Subclass function Createstar (name,sex,job) {createperson.call (this,name,sex); this.job = job;} Copy inheritance Extend (Createstar.prototype,createperson.prototype) var p2 = new Createstar ("Jay", ' Male ', ' singer ');p 2.showName ();    Jay//object-oriented copy copy to prevent content that affects the parent class object function extend (OBJ1,OBJ2) {for (Var attr in obj2) {obj1[attr] = obj2[attr];}}

Then follow the way to do a window dragging the demo, you can ruthlessly click here, to view

Ii. Other forms of inheritance (not many)

1. Class-based inheritance: Using constructors (classes) to inherit the way

/*js is not a class concept, but we can think of the constructor as a class, which is transformed from the backend * *///parent function Aaa () {this.name = ' xiaoming ';} Aaa.prototype.showName = function () {alert (this.name)}//subclass function Bbb () {}//This sentence is class-inherited Bbb.prototype = new Aaa ();//fix point problem Bbb.prototype.constructor = Bbb;var B1 = new Bbb (); B1.showname ()    ; Xiao ming

But in fact the above is still a bit problematic, if the name is an array, the parent class property will be modified, so the example is complete as follows

/* 1, JS is not the concept of class, but we can think of the constructor as a class, this way is transformed from the back end of the 2, to do property and method inheritance, to separate inherit  * *///parent function Aaa () {this.name = [];} Aaa.prototype.showName = function () {alert (this.name)}//sub-class function Bbb () {Aaa.call (this)}//this is a constructor, there is no property or method in this function , avoid attribute inheritance var F  = function () {}f.prototype = aaa.prototype;//so when B receives the prototype, it can only receive the method, cannot receive the property Bbb.prototype = new F (); Bbb.prototype.constructor = Bbb;var B1 = new BBB () B1.name.push (4); var b2 = new BBB (); alert (b2.name)    ; The

2. Prototype inheritance: Using prototypes to implement object inheritance objects

var a = {name: ' xiaoming '}var b = cloneobj (a); b.name = "Little Red" alert (a.name);    Xiao Ming Alert (b.name);  Red function cloneobj (obj) {//This method is called prototype inheritance var F = function () {}; F.prototype = Obj;return new F ();}

Summarize:

Copy inheritance: Universal , available with new or no new

Class-Type Inheritance: New constructor

Prototype inheritance: Objects without new

JS Object-oriented (ii)---inheritance

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.