Creating objects and inheriting examples in JavaScript understanding _javascript Skills

Source: Internet
Author: User
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 implement
Function myinstanceof (obj,type)
{
var proto=o bj.__proto__;
while (proto)
{
if (proto ===type.prototype) break;
proto=proto.__proto__;
}
Return proto!=null;
}


Function View () {}
Function TreeView () {}
Treeview.prototype=new view ();//treeview.proto Type.__proto__=treeview.prototype automatically completes the
treeview.prototype.constructor=treeview;//correction Constructor
Var view= New TreeView ();//view.__proto__=treeview.prototype automatically completes
alert (view instanceof view);//true finds view.__proto__.__
Alert (view instanceof TreeView) is found when proto__,//true found when view.__proto__ find
Alert (myinstanceof (View,view));//tru E
Alert (myinstanceof (View,treeview));//true

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.