JS Implementation inheritance

Source: Internet
Author: User

One, using constructors to implement "inheritance

Reference: JavaScript Object-oriented Programming (II): Inheritance of constructors

function extend (child, Parent) {

var F = function () {}; Using an empty object as a mediation F is an empty object, so it hardly accounts for memory.

F.prototype = Parent.prototype;

Child.prototype = new F ();

Child.prototype.constructor = child; Does not affect the parent

Child.uber = Parent.prototype; Spare

}

Second, the inheritance of the non-constructor function

Reference: JavaScript Object-oriented programming (c): Inheritance of non-constructors

Shallow copy

function Extendcopy (p) {

var c = {};

for (var i in P) {
C[i] = P[i];
}

C.uber = p;

return C;
}

Deep copy

function Deepcopy (p, c) {

var c = C | | {};

for (var i in P) {

if (typeof p[i] = = = ' object ') {

C[i] = (P[i].constructor = = = = Array)? [] : {};

Deepcopy (P[i], c[i]);

} else {

C[i] = P[i];

}
}

return C;
}

JS Implementation 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.