JS new function Detailed description and example tutorial

Source: Internet
Author: User
Tags eval

The eval () method is not available for random use, and using the eval () method when it is not appropriate may cause the entire program to be problematic;
and the new function () is not that big a problem. Although the new function () in any case, the constructed function works directly under the global scope, but only for syntax checking, it will not produce unexpected results because of the scope problem, as long as you do not directly call the new function constructed through it.

Increase "0," before the parameters that are accepted by eval ()
In fact, this is because there is a bug in IE. For some reason, if you want to dynamically construct a function in IE by calling Eval ().
For example:
Eval (' (function () {/* * code here/}) ');
The return obtained in IE will be undefined, while the other browsers will correctly return the reference to the newly constructed function.
The simplest and most effective solution is to add "0" to the front so that it is compatible in all major browsers.
For example:
Eval (' 0,function () {/* code here/} ');
Note: In IE9 's chakra engine, this problem has been solved.

Using the Concat method of an array object to produce a new array
You can use the form "[].concat (O)") to convert a single object parameter to an array containing only one element for processing.
For example:
Copy code code as follows:
var arr1 = [1,2];
var arr2 = [3,4];
var arr3 = Arr1.concat (ARR2);
alert (arr3.length);


Another way is: if (!) ( o instanceof array)) o = [O];
Compared with the IsArray in ECMAScript 5, it is not rigorous enough.

User reply:
1.eval really cannot be used indiscriminately;
2. In IE eval, my solution is to return in the execution function body;
3.array.prototype.concat.apply ([1,2,3],[4,5,6]);


Supplementary notes

In JS, the function below two points to note
1:
The elevation occurs when a function statement is parsed, which means that regardless of the function
Where he was placed, it would be moved to the top of the scope where it was defined
2
A statement cannot begin with a function expression because the official syntax assumes that a statement that begins with a word function is a function statement, and the solution is to enclose a function expression in a garden back to parentheses

New
The new operator of the Web effect creates an object that inherits from the prototype of its operand, and then calls the operand, binds the newly created object to this, and the operand (which should be a constructor function) an opportunity to customize the object created by returning to the requester
Without the new operator, what you do is call the normal function and bind this to the Global Object (window)


void
Void is an operator that accepts an operand and returns undefined should be as free as possible


Example
1
var foo01 = function () {
var temp = 100;
This.temp = 200;
return temp + this.temp;
}

2
var foo02 = new function ()
{
var temp = 100;
This.temp = 200;
return temp + this.temp;
}


3
var Foo3 = new function (' var temp = This.temp =; return temp + this.temp; ');

The following writing effect is the same, but notice that there is a new keyword, not a function, but a function, such as:
Document.onmouseup=new function ("flag=true");

The third is the most common one, the syntax is:
function () {statement}


-----------------------------------
One.
var foo01 = function () {
var temp = 100;
This.temp = 200;
return temp + this.temp;
}
The FOO01 function is overloaded here, and the effect is similar to the function foo01 () {statement}, but the difference is that var foo01=function () {statement} reloads the FOO01 function, which is equivalent to a model, the difference is still there, For example, there is a function that invokes the function foo01 in both ways, but the method of writing in the example inherits the FOO01, but the function foo01 () {} is executed directly, so the two characters are useful.

Two.
var foo02 = new function ()
{
var temp = 100;
This.temp = 200;
return temp + this.temp;
}
This kind of writing is not common, but it is similar to the example one, but more than the keyword new, it is obvious that the function must first define the model of the custom function, and then the function can be instantiated with the new keyword.

Three.
var Foo3 = new function (' var temp = This.temp =; return temp + this.temp; ');
It has been mentioned in the above.

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.