Object literal creates an object:
var obj = {x:[1,2],y:23};
The code is the same as below.
var obj=new Object();
obj.x=new Array(1,2);
obj.y=23;
Test:
for(var i in obj) alert(obj[i]);
Function literal: It is an expression rather than a statement.
(function(){})()
The following example:
(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.
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:
var a = 10 function(){
return 5;
}();
A bit exaggerated, 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
//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
//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
//Wrong calling method
function(x,y){
alert(x y);
return x y;
}(3,4);