Introduction to the heinous valueof method in JS

Source: Internet
Author: User
Tags function prototype

Peng Lao Wet Recent monthly report mentioned the ValueOf method, the interest came over the next ECMA5 in the ValueOf method of introduction, as follows:
15.2.4.4 Object.prototype.valueOf () when the ValueOf method is called, the following steps was Taken:1. Let O be the RE Sult of calling Toobject passing the this value as the argument. 2. If O is the result of calling the object constructor with a host object (15.2.2.1), then a. Return either O or another Value such as the host object originally passed to the constructor. The specific result, returned is implementation-defined. 3. Return O.
The explanation for valueof in the specification is short, roughly: Call the Toobject method (an abstract method, which is discussed later) and pass the value of this as a parameter.
for different arguments passed in when Toobject is called (this), the return values are as follows:
1, this is the host object, the return value depends on the browser implementation, that is, different browser return may be different (about the host object, can refer to http://www.w3school.com.cn/js/pro_js_object_types.asp)
2, this is not a host object, the value of Toobject (this) is returned

Parameter type return results
Undefined Throw TypeError exception
Null Throw TypeError exception
Number Creates a number object whose internal initial value is the passed-in parameter value
String Creates a string object whose internal initial value is the passed-in parameter value
Boolean Creates a Boolean object whose internal initial value is the passed-in parameter value
Object Returns the parameters passed in (no conversion)

The following table is available according to the definition of Object.prototype.valueOf and the description of the abstract method Toobject

OBJ type Object.prototype.valueOf.call (obj) returns the result
Undefined Throw TypeError exception
Null Throw TypeError exception
Number Number type of object with value equal to obj
String An object of type string, with a value equal to obj
Boolean A Boolean object with a value equal to obj
Object The Obj object itself

Give a few specific examples:

Copy CodeThe code is as follows: var num = 123;
Console.log (Num.valueof ()); Output: 123 Console.log (Num.valueof ()); Output: ' Number '
var unde = undefined;
Console.log (Object.prototype.valueOf.call (unde)); Output: ' Typeerror:cannot convert null to object '
var obj = {name: ' Casper '}; var linkobj = obj.valueof (); Linkobj.name = ' Change '; Console.log (Linkobj.name); Output: ' Change ' ... Description Obj.valueof () returns the object itself

In fact, the above does not mention the array, the function object, according to the following code can be guessed, when the Object.prototype.valueOf call, the parameter is an array, the function type of the object, the returned result is the object itself:

Copy CodeThe code is as follows: var arr = [1, 2, 3]; var Linkarr = arr.valueof (); Linkarr[0] = [' Casper ']; Console.log (Linkarr); Output: [' Casper ', 2, 3]
var foo = function () {return 1;}; var linkfoo = foo.valueof (); linkfoo.test = ' Casper '; Console.log (linkfoo.test); Output: ' Casper '

After reading the above description, is there a sense of epiphany? If so, congratulations, maybe you are just as well as I do not fully understand.
As a simple example, when calling Object.prototype.valueOf's object as a numeric type, it is possible to declare that the object is named Num,num in the following two ways:

Copy CodeThe code is as follows: var num = 123; DECLARE console.log (typeof num) by object literal; Output: ' number ' var num = new number (123); DECLARE console.log (typeof num) by construction method; Output: ' object '

More perverted statements, see Five ways to declare number in JS
For a description of the return value, the original text in ECMA5 is as follows :
Create a new Number object whose [[Primitivevalue]] Internal property is set to the value of the argument. See 15.7 for a description of number objects.
According to the text, it seems that num.valueof () should return a number object (that is, not a literal declaration), but actually:

Copy CodeThe code is as follows: var num = 123; var tmp = num.valueof ();
Console.log (typeof tmp); Output: ' Number '

What's going on here? And then looked carefully, it seems that some close to the truth: 5.7.4.4 Number.prototype.valueOf ()
Returns this number value.
The VALUEOF function is not generic; It throws a TypeError exception if its this value is not a number or a number object. Therefore, it cannot is transferred to and kinds of objects for use as a method.
The original number has its own prototype valueof method, not directly inherited from the Object.prototype, similar, Boolean, String also has its own prototype valueof method, summarized as follows:

Type Is there a prototype valueof method that belongs to you?
Undefined No
Null No
Number There, Number.prototype.valueOf.
String There, String.prototype.valueOf.
Boolean There, Boolean.prototype.valueOf.
Object -

Outside of this, Array, function does not have its own prototype valueof method, see specification Description:
NOTE The Array prototype object does not has a valueOf property of its own; However, it inherits the ValueOf property from the standard built-in object prototype object. The Function prototype object does not has a valueOf property of its own; However, it inherits the ValueOf property from the object prototype object. Added: Number.prototype.valueOf's internal conversion rules are slightly more complex than thought, and are not expanded here.
There are still two questions to be confused about :
1. With regard to Toobject, when the parameter is a function object, the return object does not appear to have been clearly stated in the specification, and is currently only subject to experimental speculation (and possibly I have not found)
2.valueOf of the use of the scene, the actual development has not yet seen brothers used the last of the last:
For example, please note that if you find the article useful, you can click "Recommend":)

Introduction to the heinous valueof method in JS

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.