JavaScript tips: 2.5

Source: Internet
Author: User

In the previous blog, I introduced a method that allows JavaScript to have a little bit of programming capability similar to Lambda expressions. However, because it is compatible with JavaScript syntax checks, therefore, there are limited applications that can be used.

However, some very careful friends may find that I have used a few tips in that (pseudo) Lambda module. Now I will introduce these tips:

1. Use new Function for syntax check.

Students who try to write a framework must have a deep understanding of this: Sometimes the code generated through the Code may produce syntax errors due to technical errors, leading to abnormal interruptions during the running process; sometimes you need to accept user input, but the user may intentionally or unintentionally write incorrect statements or expressions.
Some people deliberately write (or use existing) complex syntax check processes to solve this problem-but it is extremely painful to write the code used to analyze the code syntax, once the problem is found, it is very inconvenient to adjust it.
In fact, a simple solution to this problem is to use the script engine's own syntax check, for example, eval or new Function-of course, this eval cannot be used in disorder, if eval is used in an inappropriate case, the entire program will fail; the new Function is not that big-although the Function constructed by the new Function works directly in the global scope under any circumstances, however, syntax checks do not generate unexpected results due to scope issues, as long as you do not really directly call new functions constructed through it.

2. Add "0," Before the parameters accepted by eval ,".

In fact, this is because there is a BUG in IE.

For some magical reason, if you want to construct a function dynamically by calling eval in IE, add parentheses on both sides of the function expression -- for example:
Copy codeThe Code is as follows:
Eval ('(function () {/* code here */})');


The returned result is still undefined, while other browsers correctly return the reference of the newly constructed function.
After a small exploration, I found that the simplest and most effective solution to the problem is to add "0," in the front, so that it is in all (or, at least the five mainstream) the expected results are displayed in the browser.
Copy codeThe Code is as follows:
Eval ('0, function () {/* code here */}');

Note: this problem has been solved in the Chakra engine of IE9 ~

3. Use the concat method of the array object to generate a new array.

Seriously speaking, this should belong to the common sense of JavaScript. After all, the usage of this method can be found in any reference to the functions of JavaScript language.
However, some may not remember when to use it.
In my (pseudo) Lambda module, I am bored with writing a function that can simultaneously append a reference/value to multiple aliases on multiple objects, this function can take an object as a parameter or an array containing a series of objects as a parameter.
Since this function is called less frequently, I decided to use (not necessarily the best, but) a simple and effective method to adapt to the two situations, that is to say, convert a single object parameter to an array containing only one element and then process it. I chose to use "[]. concat (o) "format.
If you have read my code, you may find that the annotation contains another method, that is, "if (! (O instanceof Array) o = [o] "-- I thought about it later and thought it was not rigorous (compared with isArray in ECMAScript 5). Second, I called it less frequently, performance improvement is also limited, so we chose the previous one.
Do you have any interesting skills to share with me?

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.