JavaScript encapsulation constructors inherit non-constructor inheritance

Source: Internet
Author: User
Tags shallow copy

1 package

Encapsulates the property and method as an object and even generates an instance object from the prototype object

 1.1 Simple package:var cat1 = {}; Create an empty object

Cat1.name = "Mao"; Assigning values according to the properties of the prototype object

Cat1.color = "Yellow";

1.2 constructor prototype mode

JavaScript specifies that each constructor has a prototype property that points to another object. All properties and methods of this object are inherited by an instance of the constructor.

This means that we can define the invariant properties and methods directly prototype on the object.

function Cat (name,color) {

THIS.name = name;

This.color = color;

}

Cat.prototype.type = "Cat animal";

Cat.prototype.eat = function () {alert ("Eat Mouse")};

Generate instance var cat1 = new Cat ("Da Mao", "Yellow");

2 constructor inheritance

2.1 Using Empty object inheritance:

function extend (child, Parent) {

var F = function () {};

F.prototype = Parent.prototype;

Child.prototype = new F ();

Child.prototype.constructor = child;

Child.uber = Parent.prototype;

}

2.1 Copy Inheritance

function Extend2 (Child, Parent) {

var p = parent.prototype;

var c = Child.prototype;

for (var i in P) {

C[i] = P[i];

}

C.uber = p;

}

3 non-constructor inheritance (two ordinary objects)

3.1 Object () method

Function Object (Father) {

function F () {}

F.prototype = Father;

return new F ();

}

Use: var Doctor = object (Chinese);//Chinese Doctor's example

Then add the object to the native Doctor.career = ' doctor ';

3.2 Shallow Copy

function Extendcopy (p) {

var c = {};

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

C.uber = p;

return C;
}

Use: var Doctor = extendcopy (Chinese);

3.3 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;
}

Use: var Doctor = deepcopy (Chinese);

JavaScript encapsulation constructors inherit non-constructor inheritance

Related Article

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.