Extensions--About dynamic prototypes (JavaScript Advanced Programming) (1/3)

Source: Internet
Author: User

The reason why the inheritance mechanism cannot be dynamic is: The uniqueness of the prototype object. Instance code:
Copy code code as follows:

function A (i) {
THIS.A = i;
if (typeof a._init = = ' undefined ') {
A.prototype.func = function () {
return 0;
}
A._init = 1;
}
}
function Suba (j) {
A.call (this, 1);
THIS.J = j;
if (typeof suba._init = = ' undefined ') {
Suba.prototype = new A ();
Suba.prototype.func_sub = function () {
return ++j;
}
Suba._init = 1;
}
}
var sub_a = new Suba (1);
Alert (Sub_a.func_sub ()); Error:sub_a.func_sub is not a function

Nicholas explained that before the code was run, the object had been instantiated and contacted with prototype, and the current substitution of the prototype object would not have any effect on it, that is, the current replacement would not be accessible, and only instances of future objects would reflect that change. The first instance object will be incorrect. But the second and subsequent subclass instances are fine.

Home 1 2 3 last

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.