Eval functions: eval functions in JavaScript are one of the most controversial issues of developers. The problem is mainly caused by the possible insecurity. I will not go into details about this issue here. readers may easily browse many introductory articles. However, the advantages of the eval function are also very clear .. eval function problems
Eval functions in JavaScript are one of the most controversial issues for developers, mainly because of the possible unsecure nature. I will not go into details about this issue here. readers may easily browse many introductory articles.
However, the advantages of the eval function are also obvious. For example, if you use JS to write a calculator program and encounter a string like "2 + 1-3*5", you can easily calculate it using eval, such:
Var s = "2 + 1-3*5 ";
Console. log (eval (s ));
Problem encountered by the second mini-program trainer
(1) the applet environment does not support eval functions.
When learning the development of mini programs, I want to use a tool like a calculator to compile the trainer-I want to familiarize myself with its layout control skills. So I encountered the above problem. Naturally, I want to use the eval function, because programming is encountering a large number of strings similar to the above-including a digital expression. However, the following error occurs:
VM773: 1 Uncaught TypeError: eval is not a function (...)
(2) the new Function () solution does not work either!
After initial network exploration, I had to use the eval () function as an alternative, similar to the following:
// Calculate the expression value function eval (fn) {var Fn = Function; // A variable points to the Function to prevent some front-end compilation tools from reporting the return new Fn ('Return '+ fn) error) ();}
The result shows a failure prompt again, similar to the following:
Can not create Function
Three helpless actions
After searching for a few small program forums and comprehensive online search, you can only use the most primitive method to develop programs in a seemingly simple calculator. In summary, there are three methods:
Conversion functions, forced type conversion, and weak type conversion using js variables.
(1) conversion functions
Js provides two conversion functions: parseInt () and parseFloat. The former converts the value to an integer, and the latter converts the value to a floating point number. The two functions can run correctly only when these methods are called for the String type. NaN (Not a Number) is returned for other types ).
Some examples are as follows:
ParseInt ("1234 blue"); // returns 1234
ParseInt ("0xA"); // returns 10
ParseInt ("22.5"); // returns 22
ParseInt ("blue"); // returns NaN
The parseInt () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseInt () method, for example:
Copy the code as follows:
ParseInt ("AF", 16); // returns 175
ParseInt ("10", 2); // returns 2
ParseInt ("10", 8); // returns 8
ParseInt ("10", 10); // returns 10
If the decimal number contains the leading 0, it is best to use base 10 to avoid unexpected octal values. For example:
The code is as follows:
ParseInt ("010"); // returns 8
ParseInt ("010", 8); // returns 8
ParseInt ("010", 10); // returns 10
The parseFloat () method is similar to the parseInt () method.
Another difference between the parseFloat () method is that the string must represent a floating point number in decimal form, and parseFloat () has no base mode.
The following is an example of using parseFloat:
The code is as follows:
ParseFloat ("1234 blue"); // returns 1234.0
ParseFloat ("0xA"); // returns NaN
ParseFloat ("22.5"); // returns 22.5
ParseFloat ("22.34.5"); // returns 22.34
ParseFloat ("0908"); // returns 908
ParseFloat ("blue"); // returns NaN
(2) forced type conversion
You can also use type casting to process the type of the converted value. You can use forced type conversion to access a specific value, even if it is another type.
The three types of mandatory conversions available in ECMAScript are as follows:
Boolean (value) -- converts a given value to the Boolean type;
Number (value) -- converts a given value to a Number (which can be an integer or floating point Number );
String (value) -- converts a given value to a String.
Use one of the three functions to convert a value and store the value directly converted from the original value. This will cause unexpected consequences.
When the value to be converted is a string of at least one character, a non-zero number, or an object (this will be discussed in the next section), the Boolean () function returns true. If the value is a null string, number 0, undefined, or null, it returns false.
You can use the following code snippet to test the forced type conversion of the Boolean type.
The code is as follows:
Boolean (""); // false-empty string
Boolean ("hi"); // true-non-empty string
Boolean (100); // true-non-zero number
Boolean (null); // false-null
Boolean (0); // false-zero
Boolean (new Object (); // true-object
The forced type conversion of Number () is similar to that of parseInt () and parseFloat (), except that it converts the entire value instead of the partial value. Example:
The code is as follows:
Result through method
Number (false) 0
Number (true) 1
Number (undefined) NaN
Number (null) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object () NaN
No. (100) 100
(3) weak type conversion using js variables
Let's take a small example. at first glance, we will understand.
The code is as follows:
var str= '012.345 ';var x = str-0;x = x*1;
In the previous example, the weak type of JavaScript is used, and only arithmetic operations are performed to convert the string type to a number. This is the simplest method.
Summary
This article describes how to develop small programs. After all, a applet is designed to access local functions, so it is understandable to avoid complex functions such as eval. Currently, I have not found a common tool function on the network that can use the original method described later to calculate numeric expressions in strings. If you are interested, try it.
For more articles about the inability to use the eval function for small program development, refer to the PHP Chinese website!