Valid JavaScript Item 28 does not depend on the toString method of the function. Optional tivetostring
This series serves as the Reading Notes for objective JavaScript.
In JavaScript, there is a toString method on the function object, which can easily convert the source code of the function into a string object.
(function(x) {return x + 1;}).toString(); // "function (x) {\n return x + 1;\n}"
The toString method not only allows some hackers to find attack methods, but also has severe restrictions.
First, the toString method is not normalized by ECMAScript. Therefore, the toString implementation methods in various JavaScript Execution engines may be inconsistent.
Second, when toString can return the function source code and the function itself is fully implemented in JavaScript, the source code will be correctly returned. For example, you have called the following function and used the bind method to obtain a new function object (for how to use bind, refer to Item 25, 26 ):
(function(x) {return x + 1;}).bind(16).toString(); // "function (x) {\n [native code]\n}"
It can be found that the returned string contains [nativecode], because in many JavaScript Execution environments, the bind method is implemented using other programming languages such as C ++. Therefore, we can see that the native code actually represents a compiled C ++ source code.
Finally, the source code returned by the toString method does not reflect the value of the input parameter:
(function(x) {return function(y) {return x + y;}})(42).toString(); // "function (y) {\n return x + y;\n}"
The parameter 42 passed in the above Code is not reflected in the returned function source code.
These restrictions make it difficult to use the toString method correctly and reliably. Avoid using it in practical applications.
Summary:
- The ECMAScript standard does not regulate the toString implementation method of the function.
- Because toString is inconsistent on various platforms, try not to use it.
Strange questions about the toString () method in JavaScript
This is the case. Once you have figured it out, you will understand:
JavaScript is an object-oriented scripting language. objects are not completely object-oriented. You use
Var a = 255;
Actually, you have created a Number-type object.
Document. write (a. toString (16 ));
Actually, the toString () method of the Number object is called.
While 255 is only a numeric constant in JavaScript, so 255. toString (16) may cause a parsing error because JavaScript does not "Everything is an object", and similar usage can be used for fully object-oriented Ruby.
However, (255). toString (16) has a pair of parentheses. Before parsing. toString (16), the previous part has been converted into a Number object, so it can be called normally.
Javascript Functions
Different: toString is the Number method (var example = 111; at this time, your example is the Number type). You can call this method to convert a Number to a string, the String Type constructor uses the String Type constructor to construct a String instance of the Number type example.
Pass the rules: there is no rule, only in general cases, generally, we will directly call the method of the object to convert, which is also the most direct reason for Number to provide the toString method.
In addition, toString is a member method of the Number type, while String is a type. Don't confuse it. In my opinion, "which function is used. method. Which function uses the () method?" I don't understand what you want to ask. My understanding is: You haven't clearly understood the methods and constructor. Limited expression ability. I hope you can understand it and help you.