SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    executing functions in a for in loop

    Hi,

    I'm running a method:

    Code JavaScript:
    execute: function() {
      for (var i in TXZ.dEvent) {
        if (i != 'initilize' && i != 'execute' && typeof TXZ.dEvent[i] == 'function') {
            TXZ.dEvent[i];
          }
        }
    }

    I want to execute all functions that evaluate to true but I can't seem to get the syntax to actually run the function correctly. I know I'm near, but I can't seem to find any help on this issue online.

    Anyone know how? thanks

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To execute a function you invoke it with `()`

    TXZ.dEvent(i)

    Perhaps?
    Cross browser css bugs

    Dan Schulz you will be missed

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it throws a js error "TXZ.dEvent is not a function". I knew it couldn't have been so easy, hehe.

    EDIT:

    This seems to be the correct syntax:

    TXZ.dEvent[i]();

    I don't know why this works though if anyone cares to explain. I know that TXZ.dEvent[i] holds the value but when you add "()" it seems to execute the function (in my case). If the value is already a function then in reality what we are doing is "function() { ... }()" which doesn't make much sense.

    Can someone shed some light?

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the names around to match it with "this" and I figured it. thank you

    Code:
    this[i]();

  5. #5
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by proxi View Post
    No, it throws a js error "TXZ.dEvent is not a function". I knew it couldn't have been so easy, hehe.

    EDIT:

    This seems to be the correct syntax:

    TXZ.dEvent[i]();

    I don't know why this works though if anyone cares to explain. I know that TXZ.dEvent[i] holds the value but when you add "()" it seems to execute the function (in my case). If the value is already a function then in reality what we are doing is "function() { ... }()" which doesn't make much sense.

    Can someone shed some light?
    1. function is an object.
    2. function's name has global scope.
    3. function's name without parenthesis is a reference to the function's body.
    4. parenthesis after function's name is instruction to javascript engine to run function.

    hope this helps.


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
  •