The JavaScript valueOf () function is detailed

Source: Internet
Author: User
Tags time in milliseconds

valueOf()function to return the original value of the specified object .

This method belongs Object to an object, and since all objects have "inherited" an object instance, this method can be used by almost all instance objects.

This function is supported by all major browsers.

Grammar
Object.  ValueOf()   
return value

valueOf()The function returns the original value of the specified object.

Many of the built-in objects of JavaScript rewrite the function to make it more suitable for its own functional needs. Therefore, methods of different types valueOf() of objects can have different return and return value types.

Object return value
Array An array instance object.
Boolean Boolean value.
Date The time value stored in milliseconds, calculated from midnight UTC January 1, 1970.
Function function itself.
Number numeric value.
Object The object itself. This is the default setting.
String The string value.
Example & Description
Array: Returns the Arrays object itself
VarArray= ["Codeplayer", True, 12, -5];
Document.Writeln(Array.ValueOf() ===Array); True

Date: Current time in milliseconds from midnight January 1, 1970
VarDate= New Date(2013, 7, 18, 23, 11, 59, 230);
Document.Writeln(Date.ValueOf() ); 1376838719230

Number: Returns a numeric value
VarNum=15.26540;
Document.Writeln(Num.ValueOf() ); 15.2654

Boolean: Returns a Boolean value of TRUE or False
Varbool= True;
Document.Writeln(bool.ValueOf() ===bool); True
New A Boolean object
VarNewbool= New Boolean(True);
ValueOf () returns True, and the values of the two are equal
Document.Writeln(Newbool.ValueOf() ==Newbool); True
But not congruent, the two types are not equal, the former is a Boolean type, the latter is the object type
Document.Writeln(Newbool.ValueOf() ===Newbool); False

Function: Returns the functions themselves
functionFoo(){
}
Document.Writeln(Foo.ValueOf() ===Foo); True
VarFoo2=New Function("X", "Y", "Return x + y;");
Document.Writeln(Foo2.ValueOf() ===Foo2); True

Object: Returns the objects themselves
VarObj= {Name: "Zhang San",Age: 18};
Document.Writeln(Obj.ValueOf() ===Obj); True

String: Returns a String value
VarStr= "Http://www.365mini.com";
Document.Writeln(Str.ValueOf() ===Str //true
//new one string object
var str2 = new string ( "http.// Www.365mini.com ");
//the values are equal, but not identical, because the type is different, the former is of type string, and the latter is the object type
Document. Writeln ( Str2. Valueof () === str2 Span class= "PLN" > //false

Run code

2 We believe that: the main purpose of the user is to obtain useful information, rather than to click on the ad. Therefore, the site will strive to do a good job of content, and the separation of advertising and content, to ensure that all ads will not affect the user's normal reading experience. Users simply click on the ad with their personal wishes and interests.

The JavaScript valueOf () function is detailed

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.