JS Object Direct quantity, array direct quantity and function direct quantity

Source: Internet
Author: User

Object Direct Volume 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 (UP);
obj.y=23;

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


function Direct Amount : 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 does this explain?
You should remember that.
var a=function () {}
So how to run a, then a ()
In the same way, we do not use a variable to save so how to writing, that is
function () {} ()
But you'll find it's wrong
Because parsing engine parsing, when parsing found that the function of the end of the decision.
And didn't run that function as a block.
Then plus () is forcing the function block as a block.

First, what is anonymous function
There are three ways of defining a function in javascript:

Function-Keyword statement:
function Fnmethodname (x) {alert (x);}

function literal (functions 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 no name, that is, anonymous function. In fact, quite a few languages have anonymous functions.

The difference between the function literal and the functions () constructor
Although the function literal is an anonymous function , the syntax allows you to specify any function name for it, and you can call it yourself when writing a recursive function, but not with the function () constructor.
var f = function fact (x) {if (x < = 1) return 1; else return x*fact (x-1);};

The function () constructor allows runtime JavaScript code to be created and compiled dynamically. In this way it resembles the global function eval ().
The function () constructor parses the body of the functions each time it executes and creates a new function object. Therefore, it is very inefficient to invoke the function () constructor in a loop or in a frequently executed functions. Instead, the function literal is not recompiled every time it is encountered.
Creating a function using the function () constructor does not follow a typical scope, it always treats it as a top-level function to execute.
var y = "global"; function constructfunction () {var y = "local";  return new Function ("Return y");  Cannot get local variable}alert (constructfunction ()); Output "global" function Direct Volume:

  as long as it is an expression syntax, the script host thinks that function is a direct function, and if nothing is added, the light begins with a function and is considered to be a functional declaration that writes functions inside an expression, such as arithmetic, The host also treats it as a direct volume, as follows:


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

Exaggerate a little, 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 the direct amount of functions is one of them. like var fun = function () {}, where function does not assign a value to fun then it is an anonymous function.

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

1. function call to get return value after execution

Method One, call the function and get the return value. Forcing an operator to make a function call Execute
(function (x, y) {
alert (x+y);
return x+y;
} (3,4));

Method Two, call the function, get the return value. Forces the function to execute and then returns a reference to invoke execution
(function (x, y) {
alert (x+y);
return x+y;
}) (3,4);

2. Ignore the return value after execution

Method Three, call function, ignore return value
void function (x) {
x = x-1;
alert (x);
} (9);
Well, finally look at the wrong way to call

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

JS Object Direct quantity, array direct quantity and function direct quantity

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.