JavaScript Objects With Value - valueOf and toString
Written by Mike James   
Friday, 08 March 2013 00:00
Article Index
JavaScript Objects With Value - valueOf and toString
Functions v Objects

Functions

Now we come to a tricky area. In JavaScript functions are objects - yes they can have properties and methods. They can therefore have toString and valueOf methods. What this means is that a function can return three different values depending on how it is used.

Consider:

function myFunction() {
 return 1;
};
myFunction.valueOf = function() { return 2; }; myFunction.toString = function() { return "A"; };

After this what does:

alert(myFunction);

display? Answer: "A".

What does

alert(myFunction+0);

display?  Answer: 2.

Finally, what does:

alert(myFunction());

display? Answer: 1.

This also raises the issue of why bother using a function if general objects can return a value?

The reason is fairly obvious but deserves some thought.

Functions Versus Objects

You do have a choice of associating a value with an object as in:

myObject+1;

You also can define a function that does the same thing:

myFunction()+1;

The difference is that the function has a natural way to accept arguments. For example, you can write:

myFunction(10)+1;

but without defining it to be a function you can't write:

myObject(10)+1;

In other words, objects can simply represent a value or a state. That value or state can be manipulated by the methods that the object provides but it cannot be modified while it is being used in an expression.

A function, on the other hand, represents a relationship between input data and the result.

Notice also that, while a function is an object, not all objects are functions and in this sense a function is a "bigger" object.

When should you consider using a value associated with an object?

Some might reply "never" as it isn't a common pattern and could be confusing.

However, if an object represents data or something with state then it is a good approach.

Consider the JavaScript date object:

var d=Date.now();
alert(d);

The toString function returns the number of milliseconds since the date epoch. A Date object is an ideal example of when to use an object as a value.

 

Related Articles

A JavaScript TimeInterval object

Javascript Jems - First class functions

Overriding a JavaScript global function - parseInt

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook, install the I Programmer Toolbar or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

Banner


Task.js Asynchronous Tasks In JavaScript

There are some interesting things going on the latest version of JavaScript and already some new uses are being found for them. Task.js uses the new generator facility to build an asynchronous task fa [ ... ]



jQuery UI Custom Control - Widget Factory

So you have been using jQuery and jQuery UI but you have a few custom JavaScript controls that don't work in the same way. The solution is to create a jQuery UI custom control so that you can use ever [ ... ]


Other Articles

 

 

 

 

 

 



Last Updated ( Friday, 08 March 2013 18:51 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2013 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.