JavaScript uses eval or new function for grammar checking _javascript techniques

Source: Internet
Author: User
Tags eval
Use the new Function () for syntax checking
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]);
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.