Prototype and call (apply) combine to implement inheritance

Source: Internet
Author: User

<title>Test</title>

<script type="Text/javascript">

function Test (A, B)

{

this. A = A;

this. b = b;

this. Say = function() {

Alert ("Test-c");

}

}

Test.sum = function(A, B)

{

return New Test (this. A + A, this. B + b);

}

Test.prototype.update = function()

{

Alert ("Test-update");

}

function TestChild1 () {

this. Say = function() {

Alert ("Testchild1-c");

}

}

Testchild1.prototype = new Test (2, 3);

Testchild1.prototype = new Test ();

function TestChild2 (A, b) {

Test.call (This, a, b);

this. Say = function() {

Alert ("Testchild2-c");

}

Test.call (This, A, b);

}

Testchild2.prototype = new Test ();

var test1 = new TestChild1 ();

Test1.say ();

Test1.update ();

var test2 = new TestChild2 (1, 2);

Test2.say ();

Test2.update ();

</script>

<body>

<div id = "testprototype">test</div>

</body>

The code test above is the parent class, TestChild1 and TestChild2 are subclasses, Test1 and Test2 are instances of TestChild1 and TestChild2, respectively.

A The problem of prototype inheritance method with parameters

Testchild1.prototype = new Test (2, 3);

Testchild1.prototype = new Test ();

If TestChild1 inherits test from the previous sentence, then both A and b of all instances of TestChild1 are fixed 2 and 3.

As a result of inheritance, constructors with parameters are generally inherited by call and apply, such as the method of TestChild2 inherited test.

Two Test.call (This, a, b) the meaning of different positions

function TestChild2 (A, b) {

Test.call (This, a, b); ----Position 1

this. Say = function() {

Alert ("Testchild2-c");

}

Test.call (This, A, b); ----Position 2

}

Test.call (This, a, b); the difference between position 1 and position 2 is that, in position 1, the say function of the parent class is inherited first, and the say function in the subclass overrides the say function of the parent class, because JavaScript is executed in order. In the case of position 2, the say of the parent class is inherited first, and the subclass overrides the parent class, typically placed at position 1.

Three The combination of the two methods of inheritance

function TestChild2 (A, b) {

Test.call (This, a, b);

this. Say = function() {

Alert ("Testchild2-c");

}

Test.call (This, A, b);

}

Testchild2.prototype = new Test ();

var test2 = new TestChild2 (1, 2);

Test2.say ();

Test2.update ();

Then look at the succession method of TestChild2, it is the combination of call and prototype both.

If Testchild2.prototype = new Test () is removed, then test2.update () is unreachable because Test.call (this, a, B) is inherited only by constructors.

Four The disadvantage of the parent function is written in the constructor

The functions of the parent class are written in the constructor, such as this form.

function Test (A, B)

{

this. A = A;

this. b = b;

this. Say = function() {

Alert ("Test-c");

}

}

The Say method, which generates one instance at a time, must be generated in memory once, which is both inefficient and memory-intensive.

The best scenario is to have the say method generate only once in memory, and all instances point to that memory address. To solve this problem, use prototype, such as the following form.

Test.prototype.update = function()

{

Alert ("Test-update");

}

Five Summarize

Summary: Call to invoke the parent class with the parameter constructor, through the subclass. Prototype=new the parent class () to inherit the parent class's method. If the subclass wants to overwrite a method of the parent class, you can use TestChild2.prototype.say = function () {}.

Define the invariant properties and methods above the prototype.

Prototype and call (apply) combine to implement 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.