In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The two common methods for objects to cast are toString () and valueOf (). In fact, these two methods can also be applied to wrapper types. The toString () method has been introduced earlier, and this article will introduce the valueOf () method. If there is any primitive value, it converts the object to the original value that represents it by default; the object is a compound value, and most objects cannot really be represented as a primitive value, so the default valueOf () method simply returns the object itself rather than an original value.
[1] undefined and null have no valueOf () method
Undefined.valueOf (); / / error null.valueOf (); / / error
[2] Boolean data true and false return the original value. The wrapper object of Boolean data returns true or false
True.valueOf (); / / truetypeof true.valueOf (); / / 'boolean'false.valueOf (); / / falsetypeof false.valueOf (); / /' boolean'Boolean.valueOf (); / / Boolean () {[native code]} typeof Boolean.valueOf (); / / 'function'
[3] the original value of string type is returned. A wrapper object of type string returns a string value
'1'.valueOf (); / /' 1'''.valueOf (); / /''abc'.valueOf (); / /' abc'String.valueOf (); / / String () {[native code]} typeof String.valueOf (); / / 'function'
[4] numerical types are divided into integers and floating-point numbers for processing. A wrapper object of a numeric type returns a numeric type value.
Number.valueOf (); / / Number () {[native code]} typeof Number.valueOf (); / / 'function'
1. If the integer is directly followed by .valueof (), it will report an error and indicate an invalid mark. Because the point after the integer is recognized as a decimal point, add parentheses as far as possible.
0.valueOf (); / / Uncaught SyntaxError: Invalid or unexpected token (0) .valueOf (); / / 0+0.valueOf (); / / Uncaught SyntaxError: Invalid or unexpected token (+ 0) .valueOf (); / / 0-0.valueOf (); / / Uncaught SyntaxError: Invalid or unexpected token (- 0) .valueOf (); / /-0
[note] the valueOf () value of-0 is-0, while the toString () value of-0 is'0'.
2. The original value of floating point number is returned.
1.23.valueOf (); / / 1.23+1.23.valueOf (); / / 1.23-1.23.valueOf (); / /-1.23NaN.valueOf (); / / NaNInfinity.valueOf (); / / Infinity-Infinity.valueOf (); / /-Infinity
[note] unlike toString (), valueOf () cannot accept the conversion cardinality
[5] object Object type and custom object type return the original object
{} .valueof (); / / error report, Unexpected token. ({}). ValueOf (); / / Object {} typeof ({}). ValueOf (); / / 'object' ({avel123}). ValueOf (); / / Object {native code 123} Object.valueOf (); / Object () {[native code]} typeof Object.valueOf (); / /' function'
Function Person () {this.name = 'test';} var person1 = new Person (); person1.valueOf (); / / Person {name: "test"}
[6] the function Function type returns the original function
Function test () {alert (1); / / 1} test.valueOf (); / * function test () {alert (1); / / 1} * / Function.valueOf (); / / Function () {[native code]}
[7] the array Array type returns the original array
[] .valueof (); / / [] [1] .valueof (); / / [1] [1meme 2meme 3 native code 4] .valueof (); / / [1valueof (); / / Array () {[native code]}
[8] unlike other objects, the time Date type returns a numeric value, which is the current time value
Date.now (); / 1465115123742 (new Date ()). ValueOf (); / / 1465115123742typeof (new Date ()). ValueOf (); / / 'number'Date.valueOf (); / / Date () {[native code]}
[9] the regular expression RegExp type returns the original regular object
/ ab/i.valueOf (); / / ab/i/mom (and dad (and baby)?)? / gi.valueOf (); / mom (and dad (and baby)?)? / giRegExp.valueOf (); / / RegExp () {[native code]}
[10] wrong Error type
Error.valueOf (); / Error () {[native code]} RangeError.valueOf (); / / RangeError () {[native code]} ReferenceError.valueOf (); / / ReferenceError () {[native code]} SyntaxError.valueOf (); / / SyntaxError () {[native code]} TypeError.valueOf (); / / TypeError () {[native code]} URIError.valueOf (); / / URIError () {[native code]}
Summary
1. The main difference between toString () and valueOf () is that toString () returns a string, while valueOf () returns the original object
2. Since undefined and null are not objects, they have no toString () and valueOf () methods.
3. The toString () method of numeric Number type can accept the conversion cardinality and return values in the form of strings with different digits, while the valueOf () method cannot accept the conversion cardinality.
4. The string representation of time returned by the toString () method of type Date, while the method valueOf () returns the number of milliseconds of the numeric type between now and 00:00:00 on January 1, 1970
5. The valueOf () method of the wrapper object returns the original value corresponding to the wrapper object.
6. Use the toString () method to distinguish between built-in functions and custom functions
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.