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

Re: [jslint] Arguments class

Expand Messages
  • 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 1 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 2 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 3 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.