valueOf method

  • Thread starter florian.loitsch
  • Start date
F

florian.loitsch

hi,
I'm having troubles understanding the meaning of the "valueOf" method,
as described in the ECMA-262 (3rd Edition) standard.
I quote (section 15.2.4.4, page 85):
===
"The _valueOf_ method returns its _this_ value. If the object is the
result of calling the Object constructor with a host object
(15.2.2.1), it is implementation-defined wether _valueOf_ returns its
_this_ value or another value such as the host object originally
passed to the constructor."
===
My initial reflex was to just return "this". Obviously this wasn't the
case...
I then suspected the method to return the (internal) [[Value]]-
property of the this object. This makes sense, as the 'toValue' of a
Date-object returns an integer (which is the [[Value]]-property as
defined in 15.9.3.2).
However: it is clearly stated, that an Object created by 'new
Object()' does not have any [[Value]]-property (section 15.2.2.1):
===
"The newly constructed object has no [[Value]] property".
===
Also, I was not able to find any [[Value]]-related information for
Array or Function objects.
It seems that all available implementations return the object itself
in such cases, but is there somewhere a more precise explanation?
mfg,
// florian loitsch
 
T

Thomas 'PointedEars' Lahn

I'm having troubles understanding the meaning of the "valueOf" method,
as described in the ECMA-262 (3rd Edition) standard.
I quote (section 15.2.4.4, page 85):
===
"The _valueOf_ method returns its _this_ value. If the object is the
result of calling the Object constructor with a host object
(15.2.2.1), it is implementation-defined wether _valueOf_ returns its
_this_ value or another value such as the host object originally
passed to the constructor."
===
My initial reflex was to just return "this". Obviously this wasn't the
case...

Of course it was not. The `this' value of a method is a reference to the
object that was used to call it:

| 10.1.7 This
|
| There is a *this* value associated with every active execution context.
| The *this* value depends on the caller and the type of code being executed
| and is determined when control enters the execution context. The *this*
| value associated with an execution context is immutable.

| 10.2 Entering An Execution Context
|
| [...]
| When control enters an execution context, the scope chain is created and
| initialised, variable instantiation is performed, and the *this* value is
| determined.
|
| The initialisation of the scope chain, variable instantiation, and the
| determination of the *this* value depend on the type of code being
| entered.

| 10.2.3 Function Code
|
| [...]
| - The caller provides the *this* value. If the *this* value provided by
| the caller is not an object (including the case where it is null), then
| the *this* value is the global object.
Also, I was not able to find any [[Value]]-related information for
Array or Function objects.

| 15.3.4 Properties of the Function 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.

| 15.4.4 Properties of the Array Prototype Object
|
| [...]
| NOTE The Array prototype object does not have a valueOf property of its
| own; however, it inherits the valueOf property from the Object prototype
| Object.



HTH

PointedEars
 
F

florian.loitsch

Of course it was not. The `this' value of a method is a reference to the
object that was used to call it:
Obviously I meant the 'this' keyword (which is the correct answer). I
was
just confused, as strings and numbers where not returning the this-
value.
I just forgot to verify if they had "overwritten" methods.
Stupid...
thanks
// florian
| 10.1.7 This
|
| There is a *this* value associated with every active execution context.
| The *this* value depends on the caller and the type of code being executed
| and is determined when control enters the execution context. The *this*
| value associated with an execution context is immutable.

| 10.2 Entering An Execution Context
|
| [...]
| When control enters an execution context, the scope chain is created and
| initialised, variable instantiation is performed, and the *this* value is
| determined.
|
| The initialisation of the scope chain, variable instantiation, and the
| determination of the *this* value depend on the type of code being
| entered.

| 10.2.3 Function Code
|
| [...]
| - The caller provides the *this* value. If the *this* value provided by
| the caller is not an object (including the case where it is null), then
| the *this* value is the global object.
Also, I was not able to find any [[Value]]-related information for
Array or Function objects.

| 15.3.4 Properties of the Function 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.

| 15.4.4 Properties of the Array Prototype Object
|
| [...]
| NOTE The Array prototype object does not have a valueOf property of its
| own; however, it inherits the valueOf property from the Object prototype
| Object.

HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top