[Javascript] valueOf Object method

Returns the primitive value of the specified object.
返回指定对象的基础类型。

object.valueOf()
    Parameters : 参数
  • Array
    Returns the array instance.
  • Boolean
    The boolean value.
  • Date
    The stored time value in milliseconds midnight, January 1, 1970 UTC.
  • Function
    The function itself.
  • Number
    The numeric value.
  • Object
    The object itself. By default.
  • String
    The string value.
    Example : 例子

Take one of the former JS practice as an example, the Number-like Counter class. So instead of returning a counter object, it will return a numeric value.

function Counter(){
  this.value = 0;
};

Counter.prototype.incr = function() {
  this.value++;
};

Counter.prototype.valueOf = function () {
  return this.value;
};

Ref. MSDN

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s