www.webdeveloper.com
Results 1 to 6 of 6

Thread: Get object property name

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    2,984

    Get object property name

    It seems like there must be a simple way to do thing but I can't think of it.

    I have a simple debugger that looks like this:

    Code:
    levels = {
      'none' : 0,
      'message' : 1,
      'notice' : 2,
      'warning' : 3,
      'error' : 4
    };
    
    app.log = function(level,msg) {
      console.log('??? : '+msg);
    }
    
    //the code to fire the event
    app.log(levels.warning,'A warning message');
    Where the question marks are in the code above I want to say 'warning'....how do I do that?

    I am open to the possibility that there is a more elegant way to code this is well.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    With a for in ?
    Code:
    app.log = function(level,msg) {var prop;
    	for (prop in levels) if (levels[prop]==level) break;
    	console.log(prop+' : '+msg);
    }

  3. #3
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    You could also index the levels by number.

    Code:
    var levelsByNumber = {};
    for (var levelName in levels) {
        var levelNumber = levels[levelName];
    
        levelsByNumber[levelNumber] = levelName;
    }
    Then you could write:

    Code:
    console.log(levelsByNumber[level]+' : '+msg);
    Though, whether you ultimately use an index or a loop, unless there's more code we haven't seen, I'd say that representing levels using a number costs more than it saves. You'd be better off just passing a string.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    Both methods above will work, although I'd hate to run a for loop every time to get the property name. I ended up going Jeff's route in any case. I figured there'd be an easier way...
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    That's obviously right. Both methods use a for in loop. There is perhaps not usefull to declare the variable levelNumber for each level ?

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Just the obvious suggestion:

    Code:
    app.log = function(level,msg) {
      console.log(['none', 'message', 'notice', 'warning', 'error'][level] + ' : '+msg);
    }
    ... or using a different object:

    Code:
    levels = {
      none    : 'None',
      message : 'Message',
      notice  : 'Notice',
      warning : 'Warning',
      error   : 'Error'
    }
    
    app.log = function(level,msg) {
      console.log(level + ' : '+msg);
    }
    
    app.log(levels.warning, 'A warning message');
    Last edited by bionoid; 01-30-2012 at 12:28 PM.
    JavaScript: Learn | Validate | Compact | bionoid

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles