SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    for (prop in object) or indexed for loop?

    I have this function which does things to something in an object:
    Code:
    dostuff(p) {
      for (prop in Thing) if (prop == p) Thing[prop].className = 'x';
    }
    which seems nice and clean. But if there is more than one argument, I can end up doing stuff like this:
    Code:
    dostuff(a,b,c) {
      for (prop in Thing) if (prop == a || prop == b || prop == c) Thing[prop].className = 'x';
    }
    If there is more than one argument, should I just be using a "normal" for loop:
    Code:
    dostuff() {
      for (var i = 0; i < arguments.length; i++) Thing[arguments[i]].className = 'x';
    }
    ? Or is there some other thing that one can do that I'm missing?

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, what is Thing? Is it an array or a NodeList? Or is it some other kind of object?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's an object, to avoid using global variables. Most of the object's properties are references to elements or constants used throughout the script. In this case, the arguments for dostuff() are of the sort Thing.box, which is a reference to an HTML element.

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you planning on passing the class as an argument, are you hard coding that, or is that just an example of what the function might do?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The class is hardcoded, the only things that get passed as arguments are properties of Thing (as strings).

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since IE versions before 5.5 don't support the "in" operator, I suggest you use the following, which is like the third function you posted, except it makes sure that the sub-object of Thing exists before trying to change a property of it.
    Code:
    function doStuff() {
      for(var i=0,l=arguments.length;i<l;i++){
        if(typeof(Thing[arguments[i]]) != 'undefined')
          Thing[arguments[i]].className = 'x';
      }
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks Kravvitz, I thought it would be that. forEach would be nice, but nobody supports it except Mozilla.

  8. #8
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome

    You can add support for it via Array.prototype. I'm not sure why you think it would be better for this though.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  9. #9
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's just because I've got used to using it PHP and I like the way it works. Adding it to Array is not worth the trouble and I've read in a couple of places that in general extending the Array object is bad practice.

  10. #10
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript's forEach() is very different from PHP's foreach(). (Read the page I linked to to see for yourself.)

    JavaScript's for-in loop is the functional equivalent of PHP's foreach().
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •