JavaScript object creation and Inheritance principle example anatomy _ Basics

Source: Internet
Author: User
Tags inheritance
Object Creation
When a function object is created, the function object produced by the functions constructor runs code similar to this:
Copy Code code as follows:

This.prototype={constructor:this};

Suppose function f
f when constructing an object in new form, the object's constructor is set to this F.prototype.constructor
If the function modifies the prototype of the function before the object is created, it affects the Construtor property of the Created object
Such as:
Copy Code code as follows:

function F () {};
F.prototype={constructor: ' 1111 '};
var o=new F ();//o.constructor=== ' 1111 ' true

Inheritance Principle
Inheritance in JavaScript is the mechanism of using a prototype chain, where each instance of the function shares the data defined in the constructor prototype property, and for one class to inherit another, the parent function instance is assigned to the prototype property of the child function. And each time the new instance object is __proto__, the private property of the object is automatically connected to the prototype of the constructor.
Instanceof is to find the private prototype property chain of an instance object to determine whether it is an instance of the specified object
Concrete Examples
Copy Code code as follows:

instanceof Implementation
function myinstanceof (obj,type)
{
var proto=obj.__proto__;
while (proto)
{
if (proto ===type.prototype) break;
proto=proto.__proto__;
}
return proto!=null;
}
function View () {}
function TreeView () {}
Treeview.prototype=new View ()//treeview.prototype.__proto__=treeview.prototype automatic completion
treeview.prototype.constructor=treeview;//Correction Constructor
var view=new TreeView ();//view.__proto__=treeview.prototype Auto Complete
Alert (view instanceof view); True to find when view.__proto__.__proto__
Alert (view instanceof TreeView); True to find when view.__proto__
Alert (myinstanceof (View,view)); True
Alert (myinstanceof (View,treeview)); True

The custom myinstanceof above is the function of a instanceof function that is implemented by oneself, because the IE Kernel Instance store prototype is not __proto__, so myinstanceof can not pass, other browsers should have no problem
Related Article

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.