A simple introduction to JavaScript constructor

Source: Internet
Author: User

A brief introduction to JavaScript's constructor:
Constructor can return an object's reference to the constructor that created it, for example:

var arr=[1,2,3];console.log (arr.constructor===array);

The output value in the above code is true, which indicates that the array object of Arr's constructor property points to its constructor array.
Perhaps the above code is too simple, and then a little bit more complex code analysis:

Object.prototype.webname= "Ant Tribe";functionShow () { This. Name= "Show function name";} Show.prototype={constructor:show, Name:"Show's prototype object's name", msg:function(){return  This. Name + This. WebName}} varprototypeobj=Show.prototype;console.log (Prototypeobj.msg ());varobj=NewPrototypeobj.constructor;console.log (obj.name);varshowobj=NewShow (); Console.log (Showobj.msg ());

Here is a comment on the above code:
1.object.prototype.webname= "Ant Tribe" adds a property named WebName to the prototype object of object, with the attribute value: Ant tribe.
2.function Show () {this.name= "Show function Name"}, creates a function, when the secondary function is used as a constructor, this only wants to create an Object object instance through the constructor, then the value of this object instance's Name property is: The show function name.
3.show.prototype={}, sets the prototype object for show to the direct amount of the following object.
4.constructor:show, point to the constructor property of the prototype object to the show function.
5.name: "Show's prototype object name", Attributes and property values, this is simple enough to count.
6.msg:function () {return this.name + This.webname}, and above is just a function.
7.var Prototypeobj=show.prototype, assigns a reference to the prototype object of the show function to the variable prototypeobj.
8.console.log (Prototypeobj.msg ()), because Prototypeobj is a reference to the prototype object of show, the output will be:The name of the prototype object of show Ant tribe, in MSG, the first this is an object instance that points to the prototype, the second this is also, but without the WebName attribute in the prototype object, the WebName property of the prototype of object is found through the prototype chain.
9.var obj=new Prototypeobj.constructor,prototypeobj is the prototype object that points to show, that is, the direct amount of the object, the constructor property of the direct amount of the object is pointing to the show function, so here is the object instance that created a show.
10.console.log (Obj.name), so the show function name will be output.
11.var showobj=new Show (), creates an object instance of show.
12.console.log (Showobj.msg ()), the This in the MSG function is an object instance that points to show, although there is also a name attribute in the prototype object, but the object itself has a higher priority, and the second this is also an object instance that points to show Still found through the prototype chain: Ant tribe, so the output is:Show function name Ant tribe。

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9185

For more information, refer to: http://www.softwhy.com/javascript/

A simple introduction to JavaScript constructor

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.