admin管理员组

文章数量:1023794

Object.prototype.valueOf.call("abc")
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}
Object.prototype.valueOf.call(new String("abc"))
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}

According to MDN JavaScript calls the valueOf method to convert an object to a primitive value.If an object has no primitive value, valueOf returns the object itself, which is displayed as: [object Object] But above valueOf is returning in different format rather than returning primitive { '0': 'a' , '1': 'b' , '2': 'c' }.Isn't it against the definition?Why is it returning in that format .Now,this has created confusion .How to know what will get returned if Object.prototype.valueOf.call(array) is called for array and other objects.

Also why the return object form {}.valueOf() gets displayed since returns from [].valueOf() method when logged displays nothing

Object.prototype.valueOf.call("abc")
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}
Object.prototype.valueOf.call(new String("abc"))
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}

According to MDN JavaScript calls the valueOf method to convert an object to a primitive value.If an object has no primitive value, valueOf returns the object itself, which is displayed as: [object Object] But above valueOf is returning in different format rather than returning primitive { '0': 'a' , '1': 'b' , '2': 'c' }.Isn't it against the definition?Why is it returning in that format .Now,this has created confusion .How to know what will get returned if Object.prototype.valueOf.call(array) is called for array and other objects.

Also why the return object form {}.valueOf() gets displayed since returns from [].valueOf() method when logged displays nothing

Share Improve this question edited May 17, 2013 at 9:37 Maizere Pathak.Nepal asked May 15, 2013 at 8:13 Maizere Pathak.NepalMaizere Pathak.Nepal 2,4114 gold badges30 silver badges41 bronze badges 8
  • I'm not sure what you're getting at, what does the json piece do? : { '0': 'a' , '1': 'b' , '2': 'c' } ? – gideon Commented May 15, 2013 at 8:19
  • Where do you get [object ObjectName] from? Likely that is the result of calling toString on the returned object (such as when doing alert(object) or console.log(object). – RobG Commented May 15, 2013 at 8:32
  • @RobG [object ObjectName] should be returned if valueOf() is called via Object.prototype.valueOf("string") – Maizere Pathak.Nepal Commented May 15, 2013 at 8:35
  • 2 No, it shouldn't. You are calling Object.prototype.valueOf and passing a string primitive as this. So it gets converted to a String object and that is what is returned. Read the spec. – RobG Commented May 15, 2013 at 8:55
  • @Maizere: Do you confuse it with Object.prototype.toString? – Bergi Commented May 15, 2013 at 9:13
 |  Show 3 more ments

2 Answers 2

Reset to default 6

Let's look at what valueOf (that's a link) does:

Let O be the result of calling ToObject passing the this value as the argument.

ToObject:

String

Create a new String object whose [[PrimitiveValue]] internal property is set to the value of the argument. See 15.5 for a description of String objects.

In other words, it simply creates a new string object with the original value, i.e. new String('abc'). Now take a look at how that is displayed in your console, and you'll notice it's the same as the .valueOf.call result.

Edit: This actually has more to do with what medium you use to view the answer. Chrome's and Firefox's dev tools display strings as their literal values (the string itself), but display string objects as if they were regular objects (by displaying their properties).

A string is just an "array" of characters with some methods on them. So the representation {'0' : 'a', '1' : 'b', '2' : 'c'} means "a in the first position, b in the second, c in the third", which is the string you asked for.

And as a final note, valueOf does not give [object ObjectName]. You may be referring to Object.prototype.toString

Perhaps I've missed what the actual question is. Here is what ECMA-262 says:

15.2.4.4 Object.prototype.valueOf ( )

When the valueOf method is called, the following steps are taken:

  1. Let O be 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 that is returned is implementation-defined.

  3. Return O.

In the expression:

Object.prototype.valueOf.call("abc")

Object.prototype.valueOf is being called with a string primitive as this. So at step 1, it is converted using the internal ToObject method.

If passed a value of Type String (which "abc" is) ToObject will return a String object.

Step 2 is irrelevant since the object is not a host object (it's a native object).

Step 3 returns the object created by toObject.

So test it:

var x = Object.prototype.valueOf.call("abc");

alert(typeof x); // object

typeof returns object because the resulting value is a String object (a nice quirk of the typeof operator). You can go further:

typeof x.match; // function
x.constructor;  // function String() {[native code]}
alert(x); // abc

All consistent with the returned value being (a reference to) a String object.

Object.prototype.valueOf.call("abc")
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}
Object.prototype.valueOf.call(new String("abc"))
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}

According to MDN JavaScript calls the valueOf method to convert an object to a primitive value.If an object has no primitive value, valueOf returns the object itself, which is displayed as: [object Object] But above valueOf is returning in different format rather than returning primitive { '0': 'a' , '1': 'b' , '2': 'c' }.Isn't it against the definition?Why is it returning in that format .Now,this has created confusion .How to know what will get returned if Object.prototype.valueOf.call(array) is called for array and other objects.

Also why the return object form {}.valueOf() gets displayed since returns from [].valueOf() method when logged displays nothing

Object.prototype.valueOf.call("abc")
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}
Object.prototype.valueOf.call(new String("abc"))
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}

According to MDN JavaScript calls the valueOf method to convert an object to a primitive value.If an object has no primitive value, valueOf returns the object itself, which is displayed as: [object Object] But above valueOf is returning in different format rather than returning primitive { '0': 'a' , '1': 'b' , '2': 'c' }.Isn't it against the definition?Why is it returning in that format .Now,this has created confusion .How to know what will get returned if Object.prototype.valueOf.call(array) is called for array and other objects.

Also why the return object form {}.valueOf() gets displayed since returns from [].valueOf() method when logged displays nothing

Share Improve this question edited May 17, 2013 at 9:37 Maizere Pathak.Nepal asked May 15, 2013 at 8:13 Maizere Pathak.NepalMaizere Pathak.Nepal 2,4114 gold badges30 silver badges41 bronze badges 8
  • I'm not sure what you're getting at, what does the json piece do? : { '0': 'a' , '1': 'b' , '2': 'c' } ? – gideon Commented May 15, 2013 at 8:19
  • Where do you get [object ObjectName] from? Likely that is the result of calling toString on the returned object (such as when doing alert(object) or console.log(object). – RobG Commented May 15, 2013 at 8:32
  • @RobG [object ObjectName] should be returned if valueOf() is called via Object.prototype.valueOf("string") – Maizere Pathak.Nepal Commented May 15, 2013 at 8:35
  • 2 No, it shouldn't. You are calling Object.prototype.valueOf and passing a string primitive as this. So it gets converted to a String object and that is what is returned. Read the spec. – RobG Commented May 15, 2013 at 8:55
  • @Maizere: Do you confuse it with Object.prototype.toString? – Bergi Commented May 15, 2013 at 9:13
 |  Show 3 more ments

2 Answers 2

Reset to default 6

Let's look at what valueOf (that's a link) does:

Let O be the result of calling ToObject passing the this value as the argument.

ToObject:

String

Create a new String object whose [[PrimitiveValue]] internal property is set to the value of the argument. See 15.5 for a description of String objects.

In other words, it simply creates a new string object with the original value, i.e. new String('abc'). Now take a look at how that is displayed in your console, and you'll notice it's the same as the .valueOf.call result.

Edit: This actually has more to do with what medium you use to view the answer. Chrome's and Firefox's dev tools display strings as their literal values (the string itself), but display string objects as if they were regular objects (by displaying their properties).

A string is just an "array" of characters with some methods on them. So the representation {'0' : 'a', '1' : 'b', '2' : 'c'} means "a in the first position, b in the second, c in the third", which is the string you asked for.

And as a final note, valueOf does not give [object ObjectName]. You may be referring to Object.prototype.toString

Perhaps I've missed what the actual question is. Here is what ECMA-262 says:

15.2.4.4 Object.prototype.valueOf ( )

When the valueOf method is called, the following steps are taken:

  1. Let O be 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 that is returned is implementation-defined.

  3. Return O.

In the expression:

Object.prototype.valueOf.call("abc")

Object.prototype.valueOf is being called with a string primitive as this. So at step 1, it is converted using the internal ToObject method.

If passed a value of Type String (which "abc" is) ToObject will return a String object.

Step 2 is irrelevant since the object is not a host object (it's a native object).

Step 3 returns the object created by toObject.

So test it:

var x = Object.prototype.valueOf.call("abc");

alert(typeof x); // object

typeof returns object because the resulting value is a String object (a nice quirk of the typeof operator). You can go further:

typeof x.match; // function
x.constructor;  // function String() {[native code]}
alert(x); // abc

All consistent with the returned value being (a reference to) a String object.

本文标签: javascriptObjectprototypevalueOf() methodStack Overflow