Inheritance of Javascript non-constructor, javascript Constructor

Source: Internet
Author: User

Inheritance of Javascript non-constructor, javascript Constructor

1. What is the inheritance of "non-constructor?

For example, there is an object called "Chinese ".

Copy codeThe Code is as follows:
Var Chinese = {nation: 'China '};

Another object is called "Doctor ".

Copy codeThe Code is as follows:
Var Doctor = {career: 'Doc '}

How can I let "Doctors" inherit "Chinese"? That is to say, how can I generate a "Chinese doctor" object?

Note that these two objects are common objects, not constructor, and cannot be used to implement "inheritance ".

Ii. object () method

Douglas Crockford, inventor of json format, proposed an object () function to achieve this.

function object(o) {    function F() {}    F.prototype = o;    return new F();  }

This object () function is actually only used to direct the prototype attribute of the Child object to the parent object so that the child object can be connected with the parent object.

When used, the first step is to generate sub-Objects Based on the parent object:

Copy codeThe Code is as follows:
Var Doctor = object (Chinese );

Then, add the attributes of the sub-object:

Copy codeThe Code is as follows:
Doctor. career = 'Doc ';

At this time, the sub-object has inherited the attributes of the parent object.

Copy codeThe Code is as follows:
Alert (Doctor. nation); // China

Iii. Shallow copy

In addition to the prototype chain, there is another idea: copying all attributes of the parent object to the sub-object can also realize inheritance.

The following function is used for copying:

  function extendCopy(p) {    var c = {};    for (var i in p) {      c[i] = p[i];    }    c.uber = p; return c; }

Write as follows:

Copy codeThe Code is as follows:
Var Doctor = extendCopy (Chinese );
Doctor. career = 'Doc ';
Alert (Doctor. nation); // China

However, there is a problem with such a copy. That is, if the attribute of the parent object is equal to an array or another object, in fact, the sub-object only obtains a memory address rather than a real copy, so there is a possibility that the parent object will be tampered.

Now, add a "Birthplace" attribute to Chinese. Its value is an array.

Copy codeThe Code is as follows:
Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];

Using the extendCopy () function, Doctor inherits Chinese.

Copy codeThe Code is as follows:
Var Doctor = extendCopy (Chinese );

Then, we add a city for Doctor's "Birthplace:

Copy codeThe Code is as follows:
Doctor. birthPlaces. push ('xiamen ');

What happened? The Chinese "Birthplace" has also been changed!

Copy codeThe Code is as follows:
Alert (Doctor. birthPlaces); // Beijing, Shanghai, Hong Kong, Xiamen
Alert (Chinese. birthPlaces); // Beijing, Shanghai, Hong Kong, Xiamen

Therefore, extendCopy () is just a copy of the basic type of data. We call this copy "shortest copy ". This is an early Method for jQuery to implement inheritance.

Iv. Deep copy

The so-called "Deep copy" means to copy arrays and objects in the true sense. Its implementation is not difficult, as long as the recursive call of "shortest 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; }

Write as follows:

Copy codeThe Code is as follows:
Var Doctor = deepCopy (Chinese );

Now, add an attribute to the parent object and the value is an array. Then, modify this attribute on the sub-object:

Copy codeThe Code is as follows:
Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];
Doctor. birthPlaces. push ('xiamen ');

In this case, the parent object will not be affected.

Copy codeThe Code is as follows:
Alert (Doctor. birthPlaces); // Beijing, Shanghai, Hong Kong, Xiamen
Alert (Chinese. birthPlaces); // Beijing, Shanghai, Hong Kong

Currently, the jQuery library uses this inheritance method.

The above is all the content of this article. I hope you will like it.

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.