Prototypes and enumerable properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Prototypes and enumerable properties

    I'd like to add a method foo() to all objects:

    Object.prototyp e.foo=function( ) {
    // whatever
    }

    The caveat, though, is that I do not want foo to be enumerable by a
    for-in loop. Can I do that? If not, is there a way to derive a
    subclass of Object for which foo is not enumerable?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Douglas Crockford

    #2
    Re: Prototypes and enumerable properties

    > I'd like to add a method foo() to all objects:[color=blue]
    >
    > Object.prototyp e.foo=function( ) {
    > // whatever
    > }
    >
    > The caveat, though, is that I do not want foo to be enumerable by a
    > for-in loop. Can I do that? If not, is there a way to derive a
    > subclass of Object for which foo is not enumerable?[/color]

    No. I think this was a design error in the language itself. The
    JavaScript VM has a DONTENUM property, but it not available to the
    programmer.

    The best you can do is exclude functions while enumerating.

    for (key in object) {
    if (typeof object[key] != 'function') {
    do your thing
    }
    }


    Comment

    Working...