SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop through object properties

    Greetings, I have this function that creates a new object
    Code:
    function Steps(){
    this.prop1 = "1";
    this.prop2 = "2";
    this.prop3 = "3";
    }
    Is there a way for me to find out the number of properties that exist in my object after I create a new instance of it.
    Code:
    function getProperties(){
    var step = new Steps;
    
    //I need to loop through all the properties of the step object. Is this possible?
    }
    Thanks.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    for (prop in step) {
    alert(this.prop.value);
    }
    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="^$">

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for(sProperty in step)
    {
    	alert(sProperty + " = " + step[sProperty]);
    }

  4. #4
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    felgall & Pepejeria, This is what I was looking for. Thanks guys.


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
  •