In JavaScript, the things about new

Source: Internet
Author: User

This article is self-aware of some of the new learning processes, and there is something wrong with wanting to point out that receiving an organization's critical education.

Fuse, in the previous period of time to learn jquery, see the source code has such a paragraph:

function (Selector, context) {    returnnew  jQuery.fn.init (selector, context);}

In a short time, for beginners like me, the feeling of information is a bit large. First, what is Jquery.fn, and secondly, what is new and what is returned by that large string, and thirdly, what is the structure of jQuery, and why it is so frustrating.

Well, after a period of research, slowly to answer.

First of all, Jquery.fn is Jquery.prototype, the equivalent of the JQuery prototype to get a vest, looks shorter.

Second, the Init method is defined in the jquery prototype, and there are a few things that might be done:

    1. Pass selector and context to jquery selector engine sizzle build element list
    2. Returns the jquery instance object

Well, that is to say, whatever the toss, Init returns the jquery instance object. Let's take a look at a description of a great God to new:

A Reference object (array, object, function, etc.) will overwrite the anonymous object created by new as long as the constructor returns (return) after the new expression. If you return a primitive type (return original type undefined when no return), then the anonymous object created by new is returned.

With this answer, let's guess that new jQuery.fn.init(selector, context) the jquery object is returned.

Next, verify that the code comes first:

functiondemo1 () {return{name:"Ichigo", FirstName:"Kurosaki"    }}functionDemo2 () {return["Bleach"];}functionDemo3 () {return function () {        varstr = "DoNothing"; }}functionDemo4 () { This. Name = "Bruce";  This. FirstName = "Wayen"; return  This. Name + "" + This. FirstName;} Console.log (NewDemo1 ());//Object {name: "Ichigo", FirstName: "Kurosaki"}Console.log (NewDemo2 ());//["Bleach"]Console.log (NewDemo3 ());//function () {var str = "doNothing";}Console.log (NewDemo4 ());//Demo4 {name: "Bruce", FirstName: "Wayen"}

You can see that the result is consistent with the description, Demo1-3 is overwritten by the return value of the constructor, Demo4 returns an original type, so the Demo4 anonymous object is returned after the new operation. To mention, the original type refers to the 5 classes of string,boolean,number,undefined,null. The test code is not posted.

Well, this new work on the back is pretty basic, but there's a definition in jquery that's a init.prototype = jQuery.fn matter of jquery instance methods and attributes, and I'm not going to express myself to those superficial insights that have been answered by a master.

, Address: http://www.zhangxinxu.com/wordpress/2013/07/jquery-%e5%8e%9f%e7%90%86-%e6%9c%ba%e5%88%b6/, interested students can go to see.

In JavaScript, the things about new

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.