The difference between Var a=new function () {} and var a=function () {} and function A () {} in JS __javascript basics

Source: Internet
Author: User
Tags object object

1.function A () {}

The definition of this method is global, even if the definition after the call, the system will not complain, it can be understood that the system in the implementation of the method to find the definition of the location to initialize.

2.var a=function () {}

The method of defining the anonymous function, if invoked before the definition, will have an error. It can be understood that only when you run to this method can you initialize the variable a, and if you do not initialize the variable A, an error is made. At this point, a represents the return value of the following anonymous function.


3.var a=new function () {}

In JavaScript, a method is treated as a class, which is defined by a class with a table that is the method itself.

But there is a special case in which, if the return value is an array, method, or other class in the method, then a does not represent the method, but rather represents the new class that is returned.

Scenario One:
var a = new function () {return "center"};
alert (a);
We run the story one code, and it returns "[Object Object]", which is equivalent to:

function X () {
return "center";
}
var a = new X ();
Alert (a): We have transformed the code of scenario one into the following.

var a = new function () {return new String ("Center")};
alert (a);
We run, we will find that the return is "center", which is why.

If a Reference object (array, object, function, etc.) is returned after the new expression, it overrides the anonymous object created by new, and returns a primitive type (return-original type undefined) if returned (returns are not in fact). Anonymous object created by new
Because the new string constructs an object, rather than a String, and the new string (x) takes an argument, then alert it returns x. So yx01 returns the object of the new String ("center"), and alert yx01 displays the "center of the circle."

Scenario Two:

var a = function () {return "center"} ();
alert (a); We run the story code and return to the display "center of the Circle", which is equivalent to:

var x = function () {return "center"};
A = x ();
alert (a); Obviously, YX02 returns the execution result value of the anonymous function, which is yx02: "Center."

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.