Summary of the prototype inheritance of JS

Source: Internet
Author: User

Consider: There is an object that you want to reuse, and the second object you want to create needs to get its functionality from the first pair of objects.

The implementation is as follows:

// the object to inherit var parent = {    name:"Papa"}; // New Object var child = object (parent); Console.log (child.name);

The object function is implemented as follows:

// prototype Inheritance function Object (o) {    function  F () {};     = o;     return New F ();};

Discuss:

You can use constructors to create a parent object, so that the "self" property and the stereotype properties of the constructor are inherited.

// Parent Constructor function Person () {    this. Name = "Jie";};  // attributes added to the prototype function () {    returnthis. name;}; var New Person (); var kid = object (papa); // Test Console.log (Kid.getname ());

Another variation of this pattern is that you can choose to inherit only the prototype object of an existing constructor.

// Parent Constructor function Person () {    this. Name = "Jie";}  // attributes added to the prototype function () {    returnthis. Name;} var kid = object (person.prototype); Console.log (typeof  kid.name);  Results "undefined" console.log (typeof Kid.getname);  Result "function"

Summary of the prototype inheritance of JS

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.