In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
[1] undefined and null have no toString () method
Undefined.toString (); / / error null.toString (); / / error
[2] Boolean data true and false return the corresponding 'true' and' false'
True.toString (); / / 'true'false.toString (); / /' false'Boolean.toString (); / / "function Boolean () {[native code]}"
[3] string type original value is returned
'1'.toString (); / /' 1'''.toString (); / /''abc'.toString (); / /' abc'String.toString (); / / "function String () {[native code]}"
[4] the case of numerical types is more complex
Number.toString (); / / "function Number () {[native code]}"
1. Positive floating point numbers and NaN, Infinity,-Infinity are returned in quotation marks
1.23.toString (); / '1.23'NaN.toString (); / /' NaN'Infinity.toString (); / / 'Infinity'-Infinity.toString (); / /'-Infinity'
2. Negative floating point numbers or positive floating point numbers with'+ 'sign directly follow .toString (), toString () is invalid and returns the original value
+ 1.23.toString (); / / 1.23typeof + 1.23.toString (); / / 'number'-1.23.toString (); / /-1.23typeof-1.23.toString (); / /' number'
3. The integer directly follows the form of .toString (), which will report an error and indicate an invalid mark, because the point after the integer will be recognized as the decimal point.
0.toString (); / / Uncaught SyntaxError: Invalid or unexpected token
Therefore, in order to avoid the above invalid and error situations, numbers can be solved by adding parentheses when using the toString () method.
(0) .toString (); / /'0' (- 0) .toString (); / /'0' (+ 1.2) .toString (); / / '1.2' (- 1.2') .toString (); / /'-1.2' (NaN) .toString (); / / 'NaN'
In addition, the toString () method of a numeric type can receive an optional parameter that represents the conversion cardinality (radix), and if this parameter is not specified, the conversion rule will be based on decimal. Similarly, you can convert numbers to other binary numbers (range 2-36)
Var n = 17 n.toString (); / / '17'n.toString (2); / /' 10001'n.toString (8); / / '21'n.toString (10); / /' 17'n.toString (12); / / '15'n.toString (16); / /' 11'
[5] object Object type and custom object type are returned in parentheses [object Object]
{} .toString (); / / error, Unexpected token. ({}) .toString (); / / [object Object] ({object Object 123}) .toString (); / / [object Object] Object.toString (); / / "function Object () {[native code]}" function Person () {this.name = 'test';} var person1 = new Person (); person1.toString (); / / "[object Object]"
Type recognition
Object.prototype.toString () is often used for type identification, returning the [object data type] string representation that represents the object
[note] Object.prototype.toString () can recognize standard types and built-in object types, but not custom types
Console.log (Object.prototype.toString.call ("jerry")); / / [object String] console.log (Object.prototype.toString.call (12)); / / [object Number] console.log (Object.prototype.toString.call (true)); / / [object Boolean] console.log (Object.prototype.toString.call (undefined)); / / [object Undefined] console.log (Object.prototype.toString.call (null)) / / [object Null] console.log (Object.prototype.toString.call ({name: "jerry"})); / / [object Object] console.log (Object.prototype.toString.call (function () {})); / / [object Function] console.log (Object.prototype.toString.call ([])); / / [object Array] console.log (Object.prototype.toString.call (new Date)); / / [object Date] console.log (Object.prototype.toString.call (/\ d /)) / / [object RegExp] function Person () {}; console.log (Object.prototype.toString.call (new Person)); / / [object Object]
Function type (obj) {return Object.prototype.toString.call (obj) .slice. ToLowerCase ();} console.log (type ("jerry")); / / "string" console.log (type (12)); / / "number" console.log (type (true)); / / "boolean" console.log (type (undefined)); / / "undefined" console.log (type (null)); / / "null" console.log type ({name: "jerry"})) / / "object" console.log (type (function () {})); / / "function" console.log (type ([])); / / "array" console.log (type (new Date)); / / "date" console.log (type (/\ d /)); / / "regexp" function Person () {}; console.log (type (new Person)); / / "object"
[6] function Function type returns function code
When we call the toString () method on a custom function, we get the source code of that function; if we use the toString () method on the built-in function, we get a'[native code] 'string. Therefore, you can use the toString () method to distinguish between custom functions and built-in functions
Function test () {alert (1); / / test} test.toString (); / * "function test () {alert (1); / / test}" * / Function.toString (); / / "function Function () {[native code]}"
[7] the array Array type returns a comma-separated string concatenated by the string of each value in the array
[] .toString (); / /''[1] .toString (); / /'1' [1Yui 2je 3je 4] .toString (); / / '1Jing 2Jing 3Jing 4pointer Array.toString (); / / "function Array () {[native code]}"
[8] the time Date type returns a string representation of the time of the current time zone
(new Date ()) .toString (); / / "Sun Jun 05 2016 10:04:53 GMT+0800 (China Standard time)" Date.toString (); / / "function Date () {[native code]}"
[9] the regular expression RegExp type returns a string representation of the literal amount of the regular expression
/ ab/i.toString (); / /'/ ab/i'/mom (and dad (and baby)?)? / gi.toString (); / / 'mom (and dad (and baby)?)? / gi'RegExp.toString (); / / "function RegExp () {[native code]}"
[10] wrong Error type
Error.toString (); / / "function Error () {[native code]}" RangeError.toString (); / / "function RangeError () {[native code]}" ReferenceError.toString (); / / "function ReferenceError () {[native code]}" SyntaxError.toString (); / / "function SyntaxError () {[native code]}" TypeError.toString (); / / "function TypeError () {[native code]}" URIError.toString () / / "function URIError () {[native code]}"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.