The outrageous valueof method in JS introduces _javascript skills

Source: Internet
Author: User
Tags function prototype
Peng Old wet Recent monthly report mentioned valueof method, the interest came to the next ECMA5 in the ValueOf method of introduction, as follows:

15.2.4.4 Object.prototype.valueOf ()
When the valueof is called, the following steps are taken:
1. Let O is the result 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 is implementation-defined.
3. Return O.
The explanation for valueof in the specification is very short, roughly: Call the Toobject method (an abstract method, as described later), and pass the value of this as a parameter.

for different parameters passed in when calling Toobject (this), the return values are asUnder

1, this is the host object, the return value depends on the browser's implementation, that is, the return of different browsers may be different (for host objects, refer to http://www.w3school.com.cn/js/pro_js_object_types.asp)
2, this is not a host object, returns the value of Toobject (this)
Parameter type return results
Undefined Throw TypeError exception
Null Throw TypeError exception
Number Creates a number object that has an internal initial value of an incoming parameter value
String Creates a string object with an internal initial value of the parameter value passed in
Boolean Creates a Boolean object that has an internal initial value of the parameter value passed in
Object Returns the parameters passed in (no conversion)

The following table is available based on 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 Object of number type with value equal to obj
String Object of type string with value equal to obj
Boolean A Boolean object with a value equal to obj
Object Obj object itself

Give a few concrete examples:
Copy Code code 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, there is no mention of an array or function object, and according to the following code, it can be assumed that when the Object.prototype.valueOf call, the argument is an array, a function-type object, and the returned result is the object itself:
Copy Code code 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 kind of sudden feeling? If so, congratulations, maybe you and I have not fully understood.

As a simple example, when an object calling Object.prototype.valueOf is a numeric type, it is possible to declare that the object is named Num,num in the following two ways:
Copy Code code as follows:

var num = 123; Declares Console.log (typeof num) by object literal; Output: ' Number '
var num = new number (123); Declares Console.log (typeof num) by constructing a method; Output: ' object '

More abnormal declaration way, see "JS unknown five kinds of way to declare number"

For a description of the return value, the ECMA5 inside the text below

Create a new Number object whose [[Primitivevalue]] Internal property is set to the value of the argument. 15.7 for a description of number objects.
According to the description of this text, it seems that num.valueof () should return a number object (that is, not a literal declaration), but in fact:
Copy Code code as follows:

var num = 123;
var tmp = num.valueof ();
Console.log (typeof tmp); Output: ' Number '

What the hell is going on here? So he looked at it carefully, and it seemed to be approaching 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 it is not a number or a number object. Therefore, it cannot be transferred to the other kinds of the objects for use as a.
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, summed up as follows:
Type Do you have 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 -
In addition to this, Array, function does not have its own prototype valueof method, see specification Description:

Note The Array prototype object does not have 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 have a valueof property of its own; However, it inherits the ValueOf property from the object prototype object.
Add: Number.prototype.valueOf's internal conversion rules are a little more complicated than you think, and do not unfold here.

It's a chase, and now there are two questions to be wondered about.

1. With regard to Toobject, when the parameter is a function object, the return object does not seem to have been clearly stated in the specification, it is currently only by experimental guessing (it may be I did not find it)
2.valueOf use of the scene, the actual development has not seen a brother used
Last and Last:

For example, there are errors, please point out, if you think the article is useful to you, you can click on the "recommended":)

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.