Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] Re: Arguments class

Expand Messages
  • Mark Volkmann
    On Mon, Feb 22, 2010 at 11:38 AM, Douglas Crockford ... Admittedly my attempt at finding the answer myself failed, but I did put a fair amount of effort into
    Message 1 of 7 , Feb 22, 2010
    • 0 Attachment
      On Mon, Feb 22, 2010 at 11:38 AM, Douglas Crockford
      <douglas@...>wrote:

      > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
      > Volkmann <r.mark.volkmann@...> wrote:
      > > Not a JSLint question, but I know there are really knowledgeable
      > > JavaScript developers on this list ...
      > >
      > > Why can't I add methods to the prototype of the Arguments class?
      > > For that matter, why does the following line result in "Arguments is
      > > not defined"?
      > > print(Arguments.prototype);
      > > I thought Arguments is considered one of the core classes.
      >
      > First some straight talk. This stuff is documented. It is easy enough to
      > look it up. You should have listened to Ex-Governor Palin when she said "So,
      > in honor of the American soldier, ya quit making things up."
      >

      Admittedly my attempt at finding the answer myself failed, but I did put a
      fair amount of effort into it by googling and reading "JavaScript: The
      Definitive Guide" in addition to "JavaScript: The Good Parts".


      > > What I'd like to do is add a toArray method so I can do this when I
      > > want to call Array methods on arguments.
      > > var args = arguments.toArray();
      > > For now I just added a Array.fromArguments function that takes an
      > > Arguments object and returns an Array so I can do this instead:
      > > var args = Array.fromArguments(arguments);
      >
      > The arguments thing is a problem. It will be deprecated in a future
      > edition, replaced possibly with a splat operator.
      >
      > In ES3, arguments is an object that inherits from Object.prototype. In ES5,
      > arguments is an object that inherits from Array.prototype. So with ES5, you
      > can use arguments.slice() to produce a true arguments array.
      >

      I need a solution that works in Firefox.

      In "The Definitive Guide" I see a long description of the Arguments class in
      the "Core JavaScript Reference" section. It shows that Object is the
      prototype of Arguments objects. I don't see any hint that Arguments is
      special or different in any way from say the Array class. Why is it that I
      can add a method to the Array prototype, but I can't do the same with the
      Arguments class? Is it special in some way?


      > I recommend that you not hang stuff on Array or any of the other global
      > objects. The Prototype library does that, creating interoperability
      > problems.
      >

      I'll take that into consideration, but I'd still like to know if there is
      something "different" about the Arguments class in a Firefox environment.

      --
      R. Mark Volkmann
      Object Computing, Inc.


      [Non-text portions of this message have been removed]
    • Randy Cox
      ... objects. The Prototype library ... Douglas, How can this recommendation coexist with all of the examples of augmented objects that you list in the Good
      Message 2 of 7 , Feb 22, 2010
      • 0 Attachment
        > I recommend that you not hang stuff on Array or any of the other global
        objects. The Prototype library
        > does that, creating interoperability problems.

        Douglas,

        How can this recommendation coexist with all of the examples of augmented
        objects that you list in the Good Parts book? Object.beget,
        Function.method, Function.inherits, Number.integer, String.trim,
        Array.reduce, etc. Where do you draw the line between acceptable and
        unacceptable augmentation?

        --Randy

        Randy Cox
        Senior UI Engineer
        Compendium Blogware


        [Non-text portions of this message have been removed]
      • Jeremy Vinding
        ... I d recommend that you don t modify objects that don t belong to you. No built in objects belong to you. What you describe can be accomplished thusly:
        Message 3 of 7 , Feb 22, 2010
        • 0 Attachment
          On Feb 22, 2010, at 7:22 AM, Mark Volkmann wrote:

          > Not a JSLint question, but I know there are really knowledgeable
          > JavaScript developers on this list ...
          >
          > Why can't I add methods to the prototype of the Arguments class?
          > For that matter, why does the following line result in "Arguments is
          > not defined"?
          > print(Arguments.prototype);
          > I thought Arguments is considered one of the core classes.
          >
          > What I'd like to do is add a toArray method so I can do this when I
          > want to call Array methods on arguments.
          > var args = arguments.toArray();
          > For now I just added a Array.fromArguments function that takes an
          > Arguments object and returns an Array so I can do this instead:
          > var args = Array.fromArguments(arguments);
          >
          >
          I'd recommend that you don't modify objects that don't belong to you.
          No built in objects belong to you.

          What you describe can be accomplished thusly:

          function bob() {
          var args = Array.prototype.slice.call( arguments );
          functionThatTakesAnArray( args );
          }


          --jjv
        • Mark Volkmann
          ... Thanks for that! I d still like to know though if there is something different about the Arguments class that makes it impossible to add methods to it. --
          Message 4 of 7 , Feb 22, 2010
          • 0 Attachment
            On Mon, Feb 22, 2010 at 3:45 PM, Jeremy Vinding <yui@...>wrote:

            > On Feb 22, 2010, at 7:22 AM, Mark Volkmann wrote:
            >
            > > Not a JSLint question, but I know there are really knowledgeable
            > > JavaScript developers on this list ...
            > >
            > > Why can't I add methods to the prototype of the Arguments class?
            > > For that matter, why does the following line result in "Arguments is
            > > not defined"?
            > > print(Arguments.prototype);
            > > I thought Arguments is considered one of the core classes.
            > >
            > > What I'd like to do is add a toArray method so I can do this when I
            > > want to call Array methods on arguments.
            > > var args = arguments.toArray();
            > > For now I just added a Array.fromArguments function that takes an
            > > Arguments object and returns an Array so I can do this instead:
            > > var args = Array.fromArguments(arguments);
            > >
            > >
            > I'd recommend that you don't modify objects that don't belong to you.
            > No built in objects belong to you.
            >
            > What you describe can be accomplished thusly:
            >
            > function bob() {
            > var args = Array.prototype.slice.call( arguments );
            > functionThatTakesAnArray( args );
            > }
            >

            Thanks for that! I'd still like to know though if there is something
            different about the Arguments class that makes it impossible to add methods
            to it.

            --
            R. Mark Volkmann
            Object Computing, Inc.


            [Non-text portions of this message have been removed]
          • Douglas Crockford
            ... I was anticipating that Object.beget was going to be added to the language, and it was, but the committee changed the name to Object.create, which I
            Message 5 of 7 , Feb 22, 2010
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, Randy Cox <rcox@...> wrote:
              >
              > > I recommend that you not hang stuff on Array or any of the other global
              > objects. The Prototype library
              > > does that, creating interoperability problems.
              >
              > Douglas,
              >
              > How can this recommendation coexist with all of the examples of augmented
              > objects that you list in the Good Parts book? Object.beget,
              > Function.method, Function.inherits, Number.integer, String.trim,
              > Array.reduce, etc. Where do you draw the line between acceptable and
              > unacceptable augmentation?


              I was anticipating that Object.beget was going to be added to the language, and it was, but the committee changed the name to Object.create, which I corrected in the second printing. Similarly, String.trim is now in the language.

              Adding things to prototypes can be beneficial but is risky. We have been doing this long enough now that we know the risks.

              Adding things directly to system objects like Array is not beneficial, and can interfere with future developments. ES5 adds new methods to Object because it was likely that some breakage would occur if they were added to Object.prototype, increasing the sorts of hazards that we see with hasOwnProperty.
            Your message has been successfully submitted and would be delivered to recipients shortly.