JS multiple variable definitions (object direct, array direct and function direct) _javascript tips

Source: Internet
Author: User
Tags anonymous
Object Direct amount to create an object:
Copy Code code as follows:
var obj = {x:[1,2],y:23};

the code is the same as the following.
Copy Code code as follows:

var obj=new Object ();
Obj.x=new Array (1,2);
obj.y=23;

Test:
Copy Code code as follows:

for (var i in obj) alert (obj[i]);


function Direct Quantity: it is an expression rather than a statement.
Copy Code code as follows:
(function () {}) ()

The following example:
Copy Code code as follows:

(function () {
document.write ("Some script code");
})()

var a= (function (s) {return s}) ("abc");
alert (a);
var b=function (s) {return s};
Alert (b ("abc"));

How does that explain this?
You should remember that.
var a=function () {}
So how to run a, then is a ()
In the same way, we don't go through a variable to save.
function () {} ()
But you'll find it's wrong
Because parsing the engine parsing, the parsing time found that the function was concluded
Doesn't run that function as a block.
So plus () is forcing the function that block as a block

One, what is the anonymous function
There are generally three ways to define a function in javascript:

Functions keyword (function) statement:
function Fnmethodname (x) {alert (x);} Functions literal (function literals):
var fnmethodname = function (x) {alert (x);} function () constructor:
var fnmethodname = new Function (' x ', ' Alert (x); ') The above three methods define the same method function Fnmethodname, the 1th is the most commonly used method, the latter two are to copy a function to the variable fnmethodname, and this function is not a name, that is, anonymous function. In fact, quite a few languages have anonymous functions.


the difference between function literals and functions () constructors
Although the function literal is an anonymous function, the syntax allows you to specify any function name, and you can call itself when you write a recursive function, which is not possible with the function () constructor.
var f = function fact (x) {
if (x < = 1) return 1;
else return x*fact (x-1);
};
The function () constructor allows run-time JavaScript code to be dynamically created and compiled. In this way it resembles the global function eval ().

The function () constructor resolves the body of the functions every time it executes and creates a new function object. Therefore, it is very inefficient to call function () constructors in a loop or in frequently executed functions. Instead, the literal volume of a function is not recompiled every time it is encountered.

When a function () constructor is created, it does not follow a typical scope, and it is performed as a top-level function.

Copy Code code as follows:

var y = "global";
function Constructfunction () {
var y = "local";
Return the new Function ("Return y"); Unable to get local variable}
Alert (Constructfunction () ()); Output the direct amount of the "global" function:

As long as it is an expression syntax, the script host considers the function to be a direct-measure function, and if nothing is added, the light begins with a function to assume that it is a declaration of functions, and that it is written into an expression, such as arithmetic, and the host treats it as a direct quantity as follows
Copy Code code as follows:

var a = ten + function () {
return 5;
}();

Exaggerate a little, as follows:
Copy Code code as follows:

(function () {
Alert (1);
} ) ( );
(function () {
Alert (2);
} ( ) );
void function () {
Alert (3);
}()
0, function () {
Alert (4);
}();
-function () {
Alert (5);
}();
+function () {
Alert (6);
}();
!function () {
Alert (7);
}();
~function () {
Alert (8);
}();
typeof function () {
Alert (9);
}();

There are many ways to define a function in JS, and the direct amount of the function is one of them. such as var fun = function () {}, where function does not assign value to fun then it is an anonymous function.



OK, let's see how anonymous functions are invoked.

1, after the execution of the return value of the function call
Copy Code code as follows:

Mode one, call the function, get the return value. The force operator makes the function call execute
(function (x,y) {
alert (x+y);
return x+y;
} (3,4));

Mode two, call the function, get the return value. Force the function to perform a direct amount of execution and return a reference to the calling execution
(function (x,y) {
alert (x+y);
return x+y;
}) (3,4);

2. Ignore return value after execution
Copy Code code as follows:

Mode three, call function, ignore return value
void function (x) {
x = x-1;
alert (x);
} (9);

Well, finally, look at the wrong way of calling.
Copy Code code as follows:

The wrong way to call
function (x,y) {
alert (x+y);
return x+y;
} (3,4);

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.