reading object properties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    reading object properties

    is there a simple and elegant way to get only the properties and not the methods of a Javascript object?

    sample
    Code:
    Object.prototype.aMethod = function() { … }
    var sample = { name: "value" };
    if I use for … in here, I will also get aMethod(), which I have to filter off in the loop…
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    you could use a:

    Code:
    typeof prop != 'function'
    check for that purpose ... i'm not aware of another possibility ...

    kind regards

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by gits
      ... i'm not aware of another possibility ...
      what a pity, but I guess it can’t be helped.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        :) ... the typeof check looks a bit ugly ... but we always use it for the purpose you mentioned ... and for a second look ... it looks better then, since it just works :)

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          I too couldn’t think of a better filter…

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by gits
            Code:
            typeof prop != 'function'
            check for that purpose ... i'm not aware of another possibility ...
            found another similar one
            Code:
            prop instanceof Function
            I’ve also seen
            Code:
            prop == Function

            Comment

            Working...