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

Re: ES5 and constructors

Expand Messages
  • seank_com
    From my understanding calling a constructor without using new will mena the this pointer point to the global object and all the member function and varialbes
    Message 1 of 7 , Nov 4, 2010
    • 0 Attachment
      From my understanding calling a constructor without using new will mena the this pointer point to the global object and all the member function and varialbes you think you're adding to the object will actually be added to the global namespace. JSLint imposes a pattern that constructor always begin with a capital leter and other function do not. Then it checks that functions with capital letters are always used with new, thus avoiding the problem above.

      SeanK

      --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
      >
      > I've combed the web for answers to these questions, but haven't found
      > them ... even in the ES5 spec which surely contains the answers.
      > In ES5 what is the definition of a constructor. Is it just a function
      > whose name begins with an uppercase letter?
      > Is it always an error to invoke a constructor function without using
      > the new keyword, or is that only an error in strict mode?
      >
      > --
      > R. Mark Volkmann
      > Object Computing, Inc.
      >
    • Mark Volkmann
      Thanks! I m trying to verify though that in ES5, not just in JSLint, the definition of a constructor is a function whose name starts with an uppercase
      Message 2 of 7 , Nov 4, 2010
      • 0 Attachment
        Thanks! I'm trying to verify though that in ES5, not just in JSLint, the
        definition of a constructor is "a function whose name starts with an
        uppercase letter".

        On Thu, Nov 4, 2010 at 1:04 PM, seank_com <home@...> wrote:

        >
        >
        > From my understanding calling a constructor without using new will mena the
        > this pointer point to the global object and all the member function and
        > varialbes you think you're adding to the object will actually be added to
        > the global namespace. JSLint imposes a pattern that constructor always begin
        > with a capital leter and other function do not. Then it checks that
        > functions with capital letters are always used with new, thus avoiding the
        > problem above.
        >
        > SeanK
        >
        >
        > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
        > Volkmann <r.mark.volkmann@...> wrote:
        > >
        > > I've combed the web for answers to these questions, but haven't found
        > > them ... even in the ES5 spec which surely contains the answers.
        > > In ES5 what is the definition of a constructor. Is it just a function
        > > whose name begins with an uppercase letter?
        > > Is it always an error to invoke a constructor function without using
        > > the new keyword, or is that only an error in strict mode?
        > >
        > > --
        > > R. Mark Volkmann
        > > Object Computing, Inc.
        > >
        >
        >
        >



        --
        R. Mark Volkmann
        Object Computing, Inc.


        [Non-text portions of this message have been removed]
      • Stefan Weiss
        ... In ES5 (same as in ES3), a constructor is simply a function which creates and initializes objects. Letter case has nothing to do with it. Starting function
        Message 3 of 7 , Nov 4, 2010
        • 0 Attachment
          On 04/11/10 19:42, Mark Volkmann wrote:
          > Thanks! I'm trying to verify though that in ES5, not just in JSLint, the
          > definition of a constructor is "a function whose name starts with an
          > uppercase letter".

          In ES5 (same as in ES3), a constructor is simply a function which
          creates and initializes objects. Letter case has nothing to do with it.
          Starting function names with an uppercase letter is just a common
          convention to distinguish their role as constructors from other
          non-constructor functions.


          stefan
        • Mark Volkmann
          But I read that invoking a constructor in ES5 without using the new keyword is an error. If that is true then there must be some way to distinguish between
          Message 4 of 7 , Nov 4, 2010
          • 0 Attachment
            But I read that invoking a constructor in ES5 without using the "new"
            keyword is an error. If that is true then there must be some way to
            distinguish between constructor functions and non-constructor functions. If
            the case of the first letter is how it is done, then it is more than just a
            convention in ES5.

            On Thu, Nov 4, 2010 at 3:40 PM, Stefan Weiss <weiss@...> wrote:

            >
            >
            > On 04/11/10 19:42, Mark Volkmann wrote:
            > > Thanks! I'm trying to verify though that in ES5, not just in JSLint, the
            > > definition of a constructor is "a function whose name starts with an
            > > uppercase letter".
            >
            > In ES5 (same as in ES3), a constructor is simply a function which
            > creates and initializes objects. Letter case has nothing to do with it.
            > Starting function names with an uppercase letter is just a common
            > convention to distinguish their role as constructors from other
            > non-constructor functions.
            >
            > stefan
            >
            >
            >



            --
            R. Mark Volkmann
            Object Computing, Inc.


            [Non-text portions of this message have been removed]
          • Stefan Weiss
            ... The error is not caused by calling the constructor itself, but by the attempt to use the this value as an object inside the constructor. In ES3, the
            Message 5 of 7 , Nov 9, 2010
            • 0 Attachment
              Mark Volkmann wrote:
              > But I read that invoking a constructor in ES5 without using the "new"
              > keyword is an error. If that is true then there must be some way to
              > distinguish between constructor functions and non-constructor functions. If
              > the case of the first letter is how it is done, then it is more than just a
              > convention in ES5.

              The error is not caused by calling the constructor itself, but by the
              attempt to use the "this value" as an object inside the constructor. In
              ES3, the "this value" would be coerced to the global object, but in ES5
              strict mode, that's no longer the case.

              That doesn't mean that it's possible to look at a function and tell if
              it's intended to be used as a constructor. For example, the function

              function foo() {}

              could be used as a constructor

              var bar = new foo();

              or not

              var bar = foo();

              To make the purpose of the function more obvious, we use the
              uppercase/lowercase convention. It's just a convention, though, not a
              syntactic requirement.


              stefan
            • Morgaut Alexandre Louis Marc
              A good exemple var s = String(3); // typeof s === string var s = new String(3); // typeof s === object String() can be used to convert any value into a
              Message 6 of 7 , Nov 9, 2010
              • 0 Attachment
                A good exemple

                var s = String(3); // typeof s === "string"

                var s = new String(3); // typeof s === "object"

                String() can be used to convert any value into a string while new String() will create an object which constructor is String and which toString method will return its conversion into a string value.

                In ES5 strict mode, without new, when not applied as a method on an object, the value of this can null or undefined

                You can never say for sure that a function won't be used as a constructor

                You just have some little tricks like...
                - the function has been called once with new (then you are quite sure)
                - a property has been defined on its prototype (useless on other function than constructors)
                - its first letter is upper case (convention only)
                - it has a JSDoc with the tag "@constructor" (the developers probably wants you to manage it as a constructor)
                - it is a standard builtin constructor (from JS Core, DOM, BOM, ...)

                >



                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.