'new' operator for built-in types?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Norden

    #1

    'new' operator for built-in types?

    Hi.

    Is it necessary to use the 'new' operator for built-in types like String,
    Number, RegExp, .....? The result seems to be the same with or
    without 'new'.

    For example in a line that looks like this:
    s = s.replace(RegEx p("\\s*\\b" + name + "\\b\\s*"), " ");


    Thanks,
    Dano
  • Lasse Reichstein Nielsen

    #2
    Re: 'new' operator for built-in types?

    Daniel Norden <dnordenext@gma il.comwrites:
    Is it necessary to use the 'new' operator for built-in types like String,
    Number, RegExp, .....? The result seems to be the same with or
    without 'new'.
    That depends.

    For RegExp and Function, calling it as a function and as a constructor
    does the same thing, i.e., creates a new object.

    For Date, Object, String, Boolean and Number, calling as a function
    doesn't create a new object. Instead the last four perform conversion,
    and I don't remember what Date does when called as a function. I have
    probably never used it.
    For example in a line that looks like this:
    s = s.replace(RegEx p("\\s*\\b" + name + "\\b\\s*"), " ");
    /L
    --
    Lasse Reichstein Holst Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • RobG

      #3
      Re: 'new' operator for built-in types?

      On Nov 4, 8:04 am, Daniel Norden <dnorden...@gma il.comwrote:
      Hi.
      >
      Is it necessary to use the 'new' operator for built-in types like String,
      Number, RegExp, .....? The result seems to be the same with or
      without 'new'.
      It depends on what you mean by "necessary" . Calling String() as a
      function does type conversion, calling it as part of a new expression
      creates a new object (ECMA-262 Section 15.5.1. & 15.5.2):

      var x = String();
      var y = new String();
      alert( 'x is a ' + typeof x + // String
      '\n' + 'y is a ' + typeof y); // Object

      For example in a line that looks like this:
      s = s.replace(RegEx p("\\s*\\b" + name + "\\b\\s*"), " ");
      The RegExp function is different, when called as a function it may
      behave as if called as part of a new expression, details are in
      ECMA-262 Section 15.10.3.

      | 15.10.3.1 RegExp(pattern, flags)
      | If pattern is an object R whose [[Class]] property
      | is "RegExp" and flags is undefined, then return R
      | unchanged. Otherwise call the RegExp constructor
      | (section 15.10.4.1), passing it the pattern and flags
      | arguments and return the object constructed by that constructor.


      --
      Rob

      Comment

      • Daniel Norden

        #4
        Re: 'new' operator for built-in types?

        Lasse Reichstein Nielsen wrote:
        For RegExp and Function, calling it as a function and as a constructor
        does the same thing, i.e., creates a new object.
        >
        For Date, Object, String, Boolean and Number, calling as a function
        doesn't create a new object. Instead the last four perform conversion,
        and I don't remember what Date does when called as a function. I have
        probably never used it.
        I just checked, Date() returns a timestamp string.
        Thanks for the explanation, Lasse.

        Dano

        Comment

        • Joost Diepenmaat

          #5
          Re: 'new' operator for built-in types?

          Daniel Norden <dnordenext@gma il.comwrites:
          Hi.
          >
          Is it necessary to use the 'new' operator for built-in types like String,
          Number, RegExp, .....? The result seems to be the same with or
          without 'new'.
          Not for all types, no. See the specs
          ECMAScript® 2026 language specification, 17th edition - ECMAScript is a programming language based on several technologies like JavaScript.


          The real question should be: do we need the new operator at all?

          --
          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

          Comment

          • Daniel Norden

            #6
            Re: 'new' operator for built-in types?

            RobG wrote:
            The RegExp function is different, when called as a function it may
            behave as if called as part of a new expression, details are in
            ECMA-262 Section 15.10.3.
            >
            | 15.10.3.1 RegExp(pattern, flags)
            | If pattern is an object R whose [[Class]] property
            | is "RegExp" and flags is undefined, then return R
            | unchanged. Otherwise call the RegExp constructor
            | (section 15.10.4.1), passing it the pattern and flags
            | arguments and return the object constructed by that constructor.
            Thanks, Rob, very informative.
            I'll stick to 'RegExp' instead of 'new RegExp' then.

            Dano

            Comment

            • David Mark

              #7
              Re: 'new' operator for built-in types?

              On Nov 3, 6:24 pm, Daniel Norden <dnorden...@gma il.comwrote:
              RobG wrote:
              The RegExp function is different, when called as a function it may
              behave as if called as part of a new expression, details are in
              ECMA-262 Section 15.10.3.
              >
              | 15.10.3.1 RegExp(pattern, flags)
              | If pattern is an object R whose [[Class]] property
              | is "RegExp" and flags is undefined, then return R
              | unchanged. Otherwise call the RegExp constructor
              | (section 15.10.4.1), passing it the pattern and flags
              | arguments and return the object constructed by that constructor.
              >
              Thanks, Rob, very informative.
              I'll stick to 'RegExp' instead of 'new RegExp' then.
              Why? It looks like an extra step.

              Comment

              • Daniel Norden

                #8
                Re: 'new' operator for built-in types?

                David Mark wrote:
                >I'll stick to 'RegExp' instead of 'new RegExp' then.
                >
                Why? It looks like an extra step.
                Maybe, but that's handled by the implementation, and if there's any
                performance penalty at all, it's likely negligible compared to the
                execution of the regex itself. I guess it's a matter of taste. Leaving
                the 'new' out makes the line shorter and (a little) easier to read.

                Dano

                Comment

                • David Mark

                  #9
                  Re: 'new' operator for built-in types?

                  On Nov 3, 9:35 pm, Daniel Norden <dnorden...@gma il.comwrote:
                  David Mark wrote:
                  I'll stick to 'RegExp' instead of 'new RegExp' then.
                  >
                  Why?  It looks like an extra step.
                  >
                  Maybe, but that's handled by the implementation, and if there's any
                  performance penalty at all, it's likely negligible compared to the
                  execution of the regex itself. I guess it's a matter of taste. Leaving
                  the 'new' out makes the line shorter and (a little) easier to read.
                  As for being easier to read, it seems like it has the opposite effect.

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: 'new' operator for built-in types?

                    Daniel Norden wrote:
                    Lasse Reichstein Nielsen wrote:
                    >For RegExp and Function, calling it as a function and as a constructor
                    >does the same thing, i.e., creates a new object.
                    >>
                    >For Date, Object, String, Boolean and Number, calling as a function
                    >doesn't create a new object. Instead the last four perform conversion,
                    >and I don't remember what Date does when called as a function. I have
                    >probably never used it.
                    >
                    I just checked, Date() returns a timestamp string.
                    To be precise, it should return the same as (new Date()).toUTCSt ring() at
                    the same moment in time. See ECMAScript Edition 3 Final, section 15.9.2.
                    However, in JavaScript 1.8/Gecko 1.9/Firefox 3 it returns the same as
                    (new Date()).toStrin g(), which is implementation-dependent.


                    PointedEars
                    --
                    Use any version of Microsoft Frontpage to create your site.
                    (This won't prevent people from viewing your source, but no one
                    will want to steal it.)
                    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: 'new' operator for built-in types?

                      David Mark wrote:
                      Daniel Norden wrote:
                      >David Mark wrote:
                      >>>I'll stick to 'RegExp' instead of 'new RegExp' then.
                      >>Why? It looks like an extra step.
                      >Maybe, but that's handled by the implementation, and if there's any
                      >performance penalty at all, it's likely negligible compared to the
                      >execution of the regex itself. I guess it's a matter of taste. Leaving
                      >the 'new' out makes the line shorter and (a little) easier to read.
                      >
                      As for being easier to read, it seems like it has the opposite effect.
                      Maybe he's used to Python in which case it wouldn't ;-)


                      PointedEars
                      --
                      Prototype.js was written by people who don't know javascript for people
                      who don't know javascript. People who don't know javascript are not
                      the best source of advice on designing systems that use javascript.
                      -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

                      Comment

                      • Dr J R Stockton

                        #12
                        Re: 'new' operator for built-in types?

                        In comp.lang.javas cript message <49100087.70708 01@PointedEars. de>, Tue,
                        4 Nov 2008 08:57:59, Thomas 'PointedEars' Lahn <PointedEars@we b.de>
                        posted:
                        >Daniel Norden wrote:
                        >I just checked, Date() returns a timestamp string.
                        >
                        >To be precise, it should return the same as (new Date()).toUTCSt ring() at
                        >the same moment in time.
                        It would be naive to rely on it doing that without extensive testing -
                        if testing in IE7 counts as extensive. To prove, with reasonably
                        certainty, that they return the same, one also needs to test in multiple
                        locales. US software writers are likely to be in agreement about how
                        things are done in the US locale, and even as far away as Canada (PQ
                        excepted). They are much more likely to provide divergent results for
                        far away places. But testing in one locale can provide disproof.
                        See ECMAScript Edition 3 Final, section 15.9.2.
                        That notwithstanding . Note that .toUTCString() is implementation-
                        dependent,
                        >However, in JavaScript 1.8/Gecko 1.9/Firefox 3 it returns the same as
                        >(new Date()).toStrin g(), which is implementation-dependent.
                        IIRC, all direct conversions between Date object and string are
                        undefined in 16262, and thus are likely to be implementation-dependent,
                        especially in the forwards direction.

                        --
                        (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05.
                        Web <URL:http://www.merlyn.demo n.co.uk/- w. FAQish topics, links, acronyms
                        PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/- see 00index.htm
                        Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                        Comment

                        Working...