SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop through Math object? Math[n]

    Hello
    Is there a way to loop through an object such as Math and Date for all of its internal functions?
    Something like this:
    Code:
    for (n=0; /*Math[n]?*/; n++) {
    //Math[n](); would like to get access to all functions Math.sin() Math.cos() ...
    }
    How are these objects built compared to JSON?
    Is there a way to access Json objects functions without knowing there names
    Code:
    var obj = {
    fun1: function() {},
    fun2: function() {}
    }
    Can I loop through obj in any way?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Code:
    for (property in obj) { /* Do something with obj[property] */ }

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I don't seem to get it to work, shouldn't this give me all the functions in Math then?
    Code:
    function test() {
      for (property in Math) {
        alert(Math[property].toSource);
      };
    };

  4. #4
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found this, does it mean that I cannot loop through the Math object without knowing all it's propertynames?
    In JavaScript 1.0, you can refer to an object's properties by their property name or by their ordinal index. In JavaScript 1.1 or later, however, if you initially define a property by its name, you must always refer to it by its name, and if you initially define a property by an index, you must always refer to it by its index.
    From: Mozilla Core Javascript

  5. #5
    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)
    Running that loop will process all of the properties with isEnumerable set to true. There is no way to loop through properties that have that flag set false as those have been explicitly removed from the loop.
    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="^$">

  6. #6
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do this mean that Maths properties has isEnumerable set to false? So there is no way to loop them?

    // Thanks for all help // Anton


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
  •