JavaScript Basic Learning-javascript authoritative Guide--3.8.3 object converted to original value

Source: Internet
Author: User

object is converted to the original value

object to Boolean : compare Simple All objects to Boolean are true, including wrapper class new Boolean (False) is an object instead of the original value, it will be converted to true

objects to numbers , objects to strings are more complex. Note that the local object is discussed here and does not contain the host object (for example, browser-defined objects)

All of the objects inherit two conversion methods.

The first ToString ()

Its function is to return a string that reflects the object. The default ToString () method does not return an interesting value.

Many classes define a specific version of the ToString () method:

    • the ToString () method of the arrayconverts each array element to a string and adds a comma between the elements to combine the resulting string
    • The ToString () method of the function class returns the representation of the implementation definition of the function. Typically, a user-defined function is converted to a JavaScript source code string
    • The Date class ToString () returns a readable date and time string.
    • The regexp class toString () Converts the returned RegExp object to a direct amount string that represents the regular expression.

Cases:

 1  [1,2,3].tostring () // => "
    
      style= "color: #008080;" >2 
     3  (function  (x) {f (x);}). ToString () // => "function (x) {\nf (x); \ n}"  4  5 /\d+/g.tostring () // => "/\\d+/g"  6  7  newdate (2010,0,1). toString () // = > "Fri Jan 00:00:00 GMT-0800 (PST)"  8  9  

the second function for converting an object is valueof () .

Objects are composite values, and most objects cannot really represent a primitive value. arrays, functions, and regular expressions simply inherit this default method, and the valueof () method that invokes instances of these types simply returns the object itself. The valueof method of the date class returns an internal representation: the number of milliseconds since January 1, 1970

1 var New Date (2010,0,1)23//126332800000

In general, objects can be converted to strings and objects to numbers by using the ToString () and valueof () methods. But there are also special scenes. will be mentioned later.

Object-to-string conversions:

A. If you have the ToString () method, call this method if it returns a primitive value, JS converts it to a string, and returns the result of this character. The conversion of the original value to the string.

B. If there is no ToString () or the method does not return a primitive value, then JS will call ValueOf (). If there is a call to it, if the return value is the original value. It is converted to a string.

C. If no ToString () or valueof () obtains a primitive value, a type error exception is thrown.

Object-to-numeric conversions:

A. If the object has the valueof () method, which returns a primitive value, JavaScript converts the original value to a number and returns the number

B. Otherwise, if the object has the ToString () method, which returns a raw value, JS converts the original value back to

C. Otherwise, the JS report type is wrong.

The transformation of an object is the conversion of the original value into the desired type through the function, and an error if the original value cannot be obtained.

If the operator "+" is used as an object. Then JS uses a special way to convert the object to its original value. Instead of using other arithmetic runner methods to perform object-to-number conversions, the "= =" operator is similar. If you compare an object to an original value, the conversion will follow the way the object is converted to the original value.

"+" and "= =" Apply the conversion of the object to the original value: basically, the conversion of the object to the number is applied, first call valueof ()

Date objects are more special it uses the object-to-string conversion pattern. And the original values, as described here, through the valueof () and ToString () transformations, are used directly, and are not cast to numbers and strings.

The "<" operator and other relational operators also do the conversion of the object to the original value, but to remove the special case of the Date object: Any object will first attempt to call valueof (), and then ToString (), regardless of whether the resulting raw value is directly used, It will not be converted further into numbers or strings

"+", "= =", "! = "is the only operator that performs this special string to the original value transformation

JavaScript Basic Learning-javascript authoritative Guide--3.8.3 object converted to original value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.