Home > Web Front-end > JS Tutorial > js multiple variable definitions (object literals, array literals and function literals)_javascript skills

js multiple variable definitions (object literals, array literals and function literals)_javascript skills

WBOY
Release: 2016-05-16 18:26:27
Original
1050 people have browsed it
Object literal creates an object:
Copy code The code is as follows:
var obj = {x:[1,2],y:23};

The code is the same as below.
Copy code The code is as follows:

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

Test:
Copy code The code is as follows:

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


Function literal: It is an expression rather than a statement.
Copy code The code is as follows:
(function(){})()

The following example:
Copy the code The code is 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 to explain this
Everyone should remember This way of writing
var a=function (){}
So how to run a, then it is a()
In the same way, we do not save it through the variable a, so how to write it, it is
function(){}()
But you will find that this is wrong
Because when the parsing engine parses, it finds that it has judged that the function has ended
It does not regard that function as a block To run
, then adding () forces the function block as a block

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

Function keyword (function) statement:
function fnMethodName(x){alert(x);}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 first one is the most commonly used The latter two methods copy a function to the variable fnMethodName, and this function has no name, that is, an anonymous function. In fact, quite a few languages ​​have anonymous functions.


2. The difference between function literal and Function() constructor
Although function literal is an anonymous function, the syntax allows you to specify any function name for it , you can call itself when writing a recursive function, but not using the Function() constructor.
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 at runtime. In this way it is similar to the global function eval().

The Function() constructor parses the function body and creates a new function object each time it is executed. Therefore, the efficiency of calling the Function() constructor in a loop or frequently executed function is very low. In contrast, function literals are not recompiled every time they are encountered.

When you create a function using the Function() constructor, it does not follow the typical scope. It always executes it as a top-level function.

Copy code The code is as follows:

var y = "global";
function constructFunction() {
var y = "local";
return new Function("return y"); // Unable to obtain local variable}
alert(constructFunction()()); // Output "global" function literal:

As long as it is an expression syntax, the script host will think that function is a literal function. If nothing is added and it starts with function, it will be considered a function. Declaration, write function into an expression, such as four arithmetic operations, the host will also treat it as a direct quantity, as follows:
Copy code The code is as follows:

var a = 10 function(){
return 5;
}();

A bit exaggerated, As follows:
Copy code 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 many ways to define functions in js, and function literals are one of them. For example, var fun = function(){}, if function is not assigned to fun, then it is an anonymous function.



Okay, let’s see how the anonymous function is called.

1. The function call that returns the value after execution
Copy the code The code is as follows:

//Method 1, call the function and get the return value. The coercion operator causes the function call to execute
(function(x,y){
alert(x y);
return x y;
}(3,4));

/ /Method 2: Call the function and get the return value. Force the function to be executed 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 return value after execution
Copy code The code is as follows:

//Method 3, call the function and ignore the return value
void function(x) {
x = x-1;
alert(x);
}(9);

Well, finally look at the wrong calling method
Copy the code The code is as follows:

//Wrong calling method
function(x,y){
alert(x y);
return x y;
}(3,4);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template