'new' operator for built-in types?

Discussion in 'Javascript' started by Daniel Norden, Nov 3, 2008.

  1. 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(RegExp("\\s*\\b" + name + "\\b\\s*"), " ");


    Thanks,
    Dano
     
    Daniel Norden, Nov 3, 2008
    #1
    1. Advertising

  2. Daniel Norden <> writes:

    > 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(RegExp("\\s*\\b" + name + "\\b\\s*"), " ");


    /L
    --
    Lasse Reichstein Holst Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 3, 2008
    #2
    1. Advertising

  3. Daniel Norden

    RobG Guest

    On Nov 4, 8:04 am, Daniel Norden <> wrote:
    > 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(RegExp("\\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
     
    RobG, Nov 3, 2008
    #3
  4. 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
     
    Daniel Norden, Nov 3, 2008
    #4
  5. Joost Diepenmaat, Nov 3, 2008
    #5
  6. 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
     
    Daniel Norden, Nov 3, 2008
    #6
  7. Daniel Norden

    David Mark Guest

    On Nov 3, 6:24 pm, Daniel Norden <> wrote:
    > 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.
     
    David Mark, Nov 4, 2008
    #7
  8. 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
     
    Daniel Norden, Nov 4, 2008
    #8
  9. Daniel Norden

    David Mark Guest

    On Nov 3, 9:35 pm, 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.
     
    David Mark, Nov 4, 2008
    #9
  10. 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()).toUTCString() 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()).toString(), 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>
     
    Thomas 'PointedEars' Lahn, Nov 4, 2008
    #10
  11. 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$>
     
    Thomas 'PointedEars' Lahn, Nov 4, 2008
    #11
  12. In comp.lang.javascript message <>, Tue,
    4 Nov 2008 08:57:59, Thomas 'PointedEars' Lahn <>
    posted:
    >Daniel Norden wrote:


    >> I just checked, Date() returns a timestamp string.

    >
    >To be precise, it should return the same as (new Date()).toUTCString() 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()).toString(), 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.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Nov 4, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. muser

    built in types

    muser, Nov 26, 2003, in forum: C++
    Replies:
    4
    Views:
    374
    muser
    Nov 27, 2003
  2. DaKoadMunky
    Replies:
    13
    Views:
    628
    Richard Herring
    Jun 29, 2004
  3. ma740988
    Replies:
    6
    Views:
    414
    ma740988
    Aug 26, 2004
  4. Alona
    Replies:
    2
    Views:
    653
  5. xmllmx
    Replies:
    6
    Views:
    418
    xmllmx
    Feb 3, 2010
Loading...

Share This Page