Home > Web Front-end > JS Tutorial > body text

JavaScript tips 2.5_javascript tips

WBOY
Release: 2016-05-16 18:19:42
Original
1107 people have browsed it

In the previous blog post, I introduced a method that can make JavaScript have some programming capabilities similar to Lambda expressions . However, since it needs to be compatible with JavaScript's syntax check, it can be used in many situations. limited.

However, some more attentive friends may have discovered that I used a few tricks in that (pseudo) Lambda module. Now I will introduce these tips:

1. Use new Function to perform syntax checking.

Students who try to write frameworks must have a deep feeling about this: sometimes the code generated through the code may produce grammatical errors due to technical mistakes, resulting in abnormal interruption during operation; Sometimes it is necessary to accept user input, but users may write incorrect statements or expressions intentionally or unintentionally.
Some students deliberately write (or use ready-made) complex syntax checking processes to solve this problem - but writing code to analyze code syntax is an extremely painful thing, and once the problem is discovered, it is also very difficult to adjust. inconvenient.
In fact, there is a simpler solution to this problem, which is to use the script engine's own syntax check, such as eval or new Function - of course, this eval cannot be used indiscriminately. If eval is used inappropriately, the entire program will be corrupted. There is a problem; but new Function is not such a big problem - although the function constructed by new Function works directly in the global scope under any circumstances, if it is just a syntax check, it will not cause scope problems. Unexpected result, as long as you don't actually call the new function constructed through it directly.

2. Add "0," in front of the parameters accepted by eval.

Actually, this is due to a BUG in IE.

For some magical reason, if you want to dynamically construct a function in IE by calling eval, if you just add parentheses around the expression of the function - for example:

Copy code The code is as follows:

eval('(function(){ /* code here */ })' );


still returns undefined, while other browsers will correctly return a reference to this newly constructed function.
After a little exploration, I found that the simplest and most effective way to solve the problem is to add "0," in front, so that it can be used in all (or at least the five major current) browsers. Got the expected result.
Copy code The 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 new array.

Seriously speaking, this should be common knowledge in JavaScript. After all, the usage of this method can be found in any JavaScript language function reference.
But some students may never remember when to use it.
In my (pseudo) Lambda module, I was bored and wrote a function that can attach a reference/value to multiple objects at the same time with multiple aliases. This function can accept an object as a parameter, It can also accept an array containing a sequence of objects as a parameter.
Since this function is called less often, I decided to use a (not necessarily the most efficient, but) simple and effective way to adapt to the two situations, which is to convert a single object parameter into an array containing only one element. To deal with it again, I chose to use the form of "[].concat(o)".
If you read my code, you may find that there is another way included in the comments, which is "if (!(o instanceof Array)) o = [o]" - I thought about it later , I felt that firstly it was not rigorous enough (compared with isArray in ECMAScript 5), and secondly, the number of calls was relatively small, and the performance improvement was also very limited, so I chose the former one.
For those who are reading this article, do you have any interesting techniques to share with everyone?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template