Multiple variable definitions in js (direct object volume, direct array volume, and direct function volume) _ javascript skills

Source: Internet
Author: User
Multiple variable definitions of js (Object direct volume, array direct volume, and function Direct Volume) can be referenced by you. It will be helpful for future study of js orientation and json operations. Directly create an object:

The Code is as follows:

Var obj = {x: [1, 2], y: 23 };


The Code is the same as below.

The Code is as follows:


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


Test:

The Code is as follows:


For (var I in obj) alert (obj [I]);



Function quantity: it is an expression rather than a statement.

The Code is as follows:

(Function (){})()


For example:

The Code is as follows:


(Function (){
Document. write ("some script code ");
})()

Var a = (function (s) {return s}) ("abc ");
Alert ();
Var B = function (s) {return s };
Alert (B ("abc "));


How can this be explained?
You should remember this writing.
Var a = function (){}
So how to run a? ()
In the same way, we do not use the variable to store data.
Function (){}()
But you will find that this is wrong.
Because the parsing engine found during parsing} found that the function was over.
The function is not run as a block.
Then () is to force the function block as a block.

1. What is an anonymous function?
There are three methods to define a function in Javascript:

Function keyword statement:
Function fnMethodName (x) {alert (x);} Function 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 1st methods are the most commonly used methods, the last two functions are used to copy a function to the variable fnMethodName, which has no name, namely, an anonymous function. In fact, many languages have anonymous functions.


Ii. Differences between Function literal and Function () constructor
Although a Function is an anonymous Function, the syntax allows you to specify any Function name for the Function. When writing a recursive Function, you can call it yourself. If you use Function () to construct a Function, you cannot.
Var f = function fact (x ){
If (x <= 1) return 1;
Else return x * fact (x-1 );
};
Function () constructor allows dynamic creation and compilation of Javascript code during runtime. In this way, it is similar to the global function eval ().

Function () constructor parses the Function body each time it is executed and creates a new Function object. Therefore, it is very inefficient to call Function () constructor in a loop or frequently executed Function. On the contrary, function literal is not re-compiled every time.

When using Function () to construct a Function, it does not follow the typical scope. It always treats it as a top-level Function for execution.

The Code is as follows:


Var y = "global ";
Function constructFunction (){
Var y = "local ";
Return new Function ("return y"); // The local variable cannot be obtained}
Alert (constructFunction (); // outputs the direct quantity of "global" functions:


As long as expression syntax is used, the Script Host considers function as a direct function. If nothing is added, a function declaration starts with function, write the function into an internal expression, such as the four arithmetic operations. The host also treats it as a direct amount, as follows:

The Code is as follows:


Var a = 10 + function (){
Return 5;
}();


Exaggerated:

The Code is 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 multiple methods for defining functions in js, and the direct quantity of functions is one of them. For example, var fun = function () {}. If function is not assigned to fun, it is an anonymous function.



Okay. Let's see how anonymous functions are called.

1. Call the function to obtain the returned value after execution

The Code is as follows:


// Method 1: Call the function to obtain the returned value. Forced operator for function call execution
(Function (x, y ){
Alert (x + y );
Return x + y;
} (3, 4 ));

// Method 2: Call the function to obtain the returned value. Force the function to execute directly and then return a reference. The reference is called and executed.
(Function (x, y ){
Alert (x + y );
Return x + y;
}) (3, 4 );


2. Ignore the returned value after execution

The Code is as follows:


// Method 3: Call a function and ignore the returned value
Void function (x ){
X = X-1;
Alert (x );
} (9 );


Well, let's look at the wrong call method.

The Code is as follows:


// Incorrect call Method
Function (x, y ){
Alert (x + y );
Return x + y;
} (3, 4 );

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.