SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: function & properties
-
Jul 3, 2007, 00:25 #1
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
function & properties
I would like to know the difference in Javascript between those two lines of code
Code JavaScript:oneobject.oneproperty = onefunction;
Code JavaScript:oneobject.oneproperty = onefunction();
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 3, 2007, 00:53 #2
- Join Date
- Nov 2005
- Location
- The Netherlands
- Posts
- 808
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In your second example, the function onefunction is executed immediately, due to the parentheses.
This results in oneobject.oneproperty containing the return value of the function instead of a reference to the function itself.
Example:
Code Javascript:function foo () { return false; } myObject.prop = foo (); alert (myObject.prop); // alerts 'false' myObject.prop = foo; alert (myObject.prop); // alerts the function definition
-
Jul 3, 2007, 01:58 #3
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you i see now... so when would be the first one more usefull than the second one, because i can't see any case where i would not need my function to be executed immediately...
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 3, 2007, 02:12 #4
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- Mentioned
- 25 Post(s)
- Tagged
- 1 Thread(s)
When you set up an event handler you want to attach the processing to the event and not run it until the event occurs.
For example
window.onload = start;
will run the start function when the page finishes loading and the fields that it needs to access can be accessed. If instead you run
window.onload = start();
then you will get lots of "object has no properties" errors and will assign something meaningless to do when the page finishes loading.Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
-
Jul 3, 2007, 04:39 #5
- Join Date
- Nov 2005
- Location
- The Netherlands
- Posts
- 808
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Probably never when using event handlers, unless you want to delegate which function you want to execute when the event is fired, based on certain conditions.
Example:
Code javascript:function getEvent (){ if (conditionOne == true){ return function (){ doSomething(); }; } else { return function (){ doSomethingElse(); }; } } myObject.prop = getEvent();
But this is almost never necessary (and there are, IMHO, more readable ways of accomplishing the same.).
But you are, obviously, not always dealing with event handlers. Assigning return values instead of function references is very common when writing Javascripts.
Example:
Code javascript:function toggleDisplay (){ if (conditionOne==true){ return 'block'; } else { return 'none'; } } myObject.style.display = toggleDisplay();
-
Jul 3, 2007, 05:32 #6
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thank you both for your answers i see clearly what i will be able to do with those two!
On a PHP/Java/XML/Javascript/MySQL internship right now!
Bookmarks