Object extensions becoming array content

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • F. Da Costa

    #1

    Object extensions becoming array content

    Hi,

    I've been running into something that strikes me as pretty annoying (esp
    since I don't know how to work around it).
    It concerns the extension of Object and the usage of Maps.

    Following a snip I use to extend the Object object.

    Object.prototyp e.extend = function (oSuper) {
    for (sProperty in oSuper) {
    this[sProperty] = oSuper[sProperty];
    }
    };

    This all goes fine. The function available etc.
    Here is the catch.

    When I create ab arr=new Object() to be used as a Map or an arr=[] to be
    used as a Map (as well) I can fill the arr. No problems so far.
    When I use the for (o in arr) { ... } I keep on tripping over the the fact
    that "extend" is a key with {ScriptFunction } as value.

    Obviously this is *not* what I'm looking for.
    Am I doing something wrong here and if so what?
    Trying to rewrite my code every time this comes up is starting to ...

    TIA

    F DCG
  • Lasse Reichstein Nielsen

    #2
    Re: Object extensions becoming array content

    "F. Da Costa" <dacosta@xs4all .nl> writes:
    [color=blue]
    > Following a snip I use to extend the Object object.
    >
    > Object.prototyp e.extend = function (oSuper) {
    > for (sProperty in oSuper) {
    > this[sProperty] = oSuper[sProperty];
    > }
    > };[/color]
    [color=blue]
    > When I create ab arr=new Object() to be used as a Map or an arr=[] to
    > be used as a Map (as well) I can fill the arr. No problems so far.
    > When I use the for (o in arr) { ... } I keep on tripping over the the
    > fact that "extend" is a key with {ScriptFunction } as value.[/color]

    Because it is. All your objects have "extend" as a property.
    [color=blue]
    > Obviously this is *not* what I'm looking for.[/color]

    What are you looking for, then? (I.e., no, it's not obvious).

    User added properties cannot be made non-enumerable, so anything you
    add will show up in a for(in)-loop.
    [color=blue]
    > Am I doing something wrong here and if so what?[/color]

    Try this:
    ---
    Object.prototyp e.extend = function (oSuper) {
    for (sProperty in oSuper) {
    if (oSuper.hasOwnP roperty(sProper ty)) {
    this[sProperty] = oSuper[sProperty];
    }
    }
    };
    ---
    Then you will only copy the non-inherited properties of oSuper.

    "hasOwnProperty " is a fairly new feature in ECMAScript, so older
    browsers won't understand it.


    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • F. Da Costa

      #3
      Re: Object extensions becoming array content

      Lasse Reichstein Nielsen wrote:[color=blue]
      > "F. Da Costa" <dacosta@xs4all .nl> writes:
      >
      >[color=green]
      >>Following a snip I use to extend the Object object.
      >>
      >>Object.protot ype.extend = function (oSuper) {
      >> for (sProperty in oSuper) {
      >> this[sProperty] = oSuper[sProperty];
      >> }
      >>};[/color]
      >
      >[color=green]
      >>When I create ab arr=new Object() to be used as a Map or an arr=[] to
      >>be used as a Map (as well) I can fill the arr. No problems so far.
      >>When I use the for (o in arr) { ... } I keep on tripping over the the
      >>fact that "extend" is a key with {ScriptFunction } as value.[/color]
      >
      >
      > Because it is. All your objects have "extend" as a property.[/color]
      Ok, that's clear.
      [color=blue]
      >
      >[color=green]
      >>Obviously this is *not* what I'm looking for.[/color]
      >
      >
      > What are you looking for, then? (I.e., no, it's not obvious).[/color]
      Just the verifiable content put in as data[color=blue]
      >
      > User added properties cannot be made non-enumerable, so anything you
      > add will show up in a for(in)-loop.[/color]
      This is fine, so I test for this whilst looping through an array.

      Example:
      for (var o in arr) {
      if (!!arr[o].someProp) { // the someProp *is* always in the data object
      // arr[o] *is* a proper data entry
      }
      }

      Would there be a better way to test for arr[o]=void (as per the Moz debugger)?
      The only thing I could come up with was the !![color=blue]
      >
      >[color=green]
      >>Am I doing something wrong here and if so what?[/color]
      >
      >
      > Try this:
      > ---
      > Object.prototyp e.extend = function (oSuper) {
      > for (sProperty in oSuper) {
      > if (oSuper.hasOwnP roperty(sProper ty)) {
      > this[sProperty] = oSuper[sProperty];
      > }
      > }
      > };
      > ---
      > Then you will only copy the non-inherited properties of oSuper.
      >
      > "hasOwnProperty " is a fairly new feature in ECMAScript, so older
      > browsers won't understand it.[/color]
      Thz but in that case I'll copy it into my js snips "to be inserted later".
      First make it work ...[color=blue]
      >
      >
      > /L[/color]
      F DCG

      Comment

      • Douglas Crockford

        #4
        Re: Object extensions becoming array content

        > I've been running into something that strikes me as pretty annoying (esp[color=blue]
        > since I don't know how to work around it).
        > It concerns the extension of Object and the usage of Maps.
        >
        > Following a snip I use to extend the Object object.
        >
        > Object.prototyp e.extend = function (oSuper) {
        > for (sProperty in oSuper) {
        > this[sProperty] = oSuper[sProperty];
        > }
        > };
        >
        > This all goes fine. The function available etc.
        > Here is the catch.
        >
        > When I create ab arr=new Object() to be used as a Map or an arr=[] to be
        > used as a Map (as well) I can fill the arr. No problems so far.
        > When I use the for (o in arr) { ... } I keep on tripping over the the fact
        > that "extend" is a key with {ScriptFunction } as value.
        >
        > Obviously this is *not* what I'm looking for.
        > Am I doing something wrong here and if so what?
        > Trying to rewrite my code every time this comes up is starting to ...[/color]

        I consider that to be a design error in the language. The ECMAScript model
        includes a property called DONTENUM which hides properties from the for..in
        statement. Unfortunately, there is nothing in the language that allows you to
        use it. That bites.

        I have used two alternatives:

        (1) Have a .bag member in your function object which holds the enumerated stuff.

        (2) In your for..in loop, reject function values

        for (n in o) {
        if (typeof o[n] != 'function') {
        ...
        }
        }



        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Object extensions becoming array content

          "F. Da Costa" <dacosta@xs4all .nl> writes:
          [color=blue]
          > Just the verifiable content put in as data[/color]

          I.e., Just the data you put there yourself, not the ones inherited from
          the prototype.
          [color=blue][color=green]
          >> User added properties cannot be made non-enumerable, so anything you
          >> add will show up in a for(in)-loop.[/color]
          > This is fine, so I test for this whilst looping through an array.
          >
          > Example:
          > for (var o in arr) {
          > if (!!arr[o].someProp) { // the someProp *is* always in the data object[/color]

          That's one solution. If you have a way to recognize the proper data,
          then just use that.

          You can drop the "!!". All it does is to convert its operand to a
          boolean, but it is being used inside a condition, so it will always
          be converted to a boolean.
          [color=blue]
          > Would there be a better way to test for arr[o]=void (as per the Moz
          > debugger)?[/color]

          I don't know the Moz debugger, but I'm pretty sure it's not "void".
          More likely you mean:
          arr[o]===undefined
          [color=blue]
          > The only thing I could come up with was the !![/color]

          Just plain
          if (arr[o].someProp) { /// arr[0].someProp is not undefined

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • F. Da Costa

            #6
            Re: Object extensions becoming array content

            Douglas Crockford wrote:
            [color=blue][color=green]
            >>I've been running into something that strikes me as pretty annoying (esp
            >>since I don't know how to work around it).
            >>It concerns the extension of Object and the usage of Maps.
            >>
            >>Following a snip I use to extend the Object object.
            >>
            >>Object.protot ype.extend = function (oSuper) {
            >> for (sProperty in oSuper) {
            >> this[sProperty] = oSuper[sProperty];
            >> }
            >>};
            >>
            >>This all goes fine. The function available etc.
            >>Here is the catch.
            >>
            >>When I create ab arr=new Object() to be used as a Map or an arr=[] to be
            >>used as a Map (as well) I can fill the arr. No problems so far.
            >>When I use the for (o in arr) { ... } I keep on tripping over the the fact
            >>that "extend" is a key with {ScriptFunction } as value.
            >>
            >>Obviously this is *not* what I'm looking for.
            >>Am I doing something wrong here and if so what?
            >>Trying to rewrite my code every time this comes up is starting to ...[/color]
            >
            >
            > I consider that to be a design error in the language. The ECMAScript model
            > includes a property called DONTENUM which hides properties from the for..in
            > statement. Unfortunately, there is nothing in the language that allows you to
            > use it. That bites.
            >
            > I have used two alternatives:
            >
            > (1) Have a .bag member in your function object which holds the enumerated stuff.
            >
            > (2) In your for..in loop, reject function values
            >
            > for (n in o) {
            > if (typeof o[n] != 'function') {
            > ...
            > }
            > }
            >[/color]
            Thx, I got it working by testing on a property I know exists in the object
            being retrieved.
            I do see the generic elegance though of your solution so I guess I'll do
            some stepping through. ;)
            [color=blue]
            > http://www.ecmascript.org
            >[/color]

            Comment

            • F. Da Costa

              #7
              Re: Object extensions becoming array content

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > "F. Da Costa" <dacosta@xs4all .nl> writes:
              >
              >[color=green]
              >>Just the verifiable content put in as data[/color]
              >
              >
              > I.e., Just the data you put there yourself, not the ones inherited from
              > the prototype.
              >
              >[color=green][color=darkred]
              >>>User added properties cannot be made non-enumerable, so anything you
              >>>add will show up in a for(in)-loop.[/color]
              >>
              >>This is fine, so I test for this whilst looping through an array.
              >>
              >>Example:
              >>for (var o in arr) {
              >> if (!!arr[o].someProp) { // the someProp *is* always in the data object[/color]
              >
              >
              > That's one solution. If you have a way to recognize the proper data,
              > then just use that.
              >
              > You can drop the "!!". All it does is to convert its operand to a
              > boolean, but it is being used inside a condition, so it will always
              > be converted to a boolean.
              >[/color]
              Ah, in the debugger I had to use the !! because it just looked at the
              object normally and not from a condition p.o.v.[color=blue]
              >[color=green]
              >>Would there be a better way to test for arr[o]=void (as per the Moz
              >>debugger)?[/color]
              >
              >
              > I don't know the Moz debugger, but I'm pretty sure it's not "void".
              > More likely you mean:
              > arr[o]===undefined[/color]
              I guess its a flavor thing. It did show void but the issue was as explained
              above[color=blue]
              >
              >[color=green]
              >>The only thing I could come up with was the !![/color]
              >
              >
              > Just plain
              > if (arr[o].someProp) { /// arr[0].someProp is not undefined[/color]
              So it is.
              Ths for the explanatory effort[color=blue]
              >
              > /L[/color]
              F DCG

              Comment

              Working...