Prototype inheritance in JavaScript

Source: Internet
Author: User

Prototype chain:
Object (constructor) object (Type (object))
var o = {};alert (typeof O);        // The result is Objectalert (typeof object);   // The result is function

Each object has a property called __proto__ , which is the prototype of this object (O. __proto__)

Objects have prototypes, prototypes are also objects, so prototypes have prototypes.
All the functions are objects, Inheriting from Function.prototype,function.prototype is an object, inheriting from Object.prototype,object.prototype is an object, inheriting from Nullobj is an object, inheriting from Object.prototypefunc tion is an object, inheriting from Function.prototypefunction is a function that inherits from Function.prototype
the access rules for JS members O. Method () First look for the definition of the member in the current type O, and if there is a definition of the member, use the change member directly, and if the member is no longer in the current type, access its prototype (the previous level in the prototype chain) and so on, until the null position

code example:
Do not use inheritance:
var function () {    thisfunction() {        alert ("Hello");    };} ; var New Person (); var New  = = = P2.say);   // The result is false
Each new object creates a piece of memory, so P1.say and P2.say are not the same address as the reference.

using prototype inheritance:
var function     = {    say:function() {        alert ("Mr Jing"); }}; var New Person (); var New  = "Mr Jing"= = = = P2.say);      // The result is truealert (p2.name);                // The result is "Mr Jing"

Prototype inheritance in JavaScript

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.