SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    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;
    and

    Code JavaScript:
    oneobject.oneproperty = onefunction();
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  2. #2
    SitePoint Guru Ize's Avatar
    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

  3. #3
    SitePoint Addict
    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!

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    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="^$">

  5. #5
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by motion-ex View Post
    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...
    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();

  6. #6
    SitePoint Addict
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •