Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > 'new' operator for built-in types?

Reply
Thread Tools

'new' operator for built-in types?

 
 
Daniel Norden
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-03-2008
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.'
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-03-2008
On Nov 4, 8:04*am, Daniel Norden <dnorden...@gmail.com> 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
 
Reply With Quote
 
Daniel Norden
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      11-03-2008
Daniel Norden <> writes:

> 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
http://www.ecma-international.org/pu...s/Ecma-262.htm

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

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
Daniel Norden
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-04-2008
On Nov 3, 6:24*pm, Daniel Norden <dnorden...@gmail.com> 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.
 
Reply With Quote
 
Daniel Norden
Guest
Posts: n/a
 
      11-04-2008
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
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-04-2008
On Nov 3, 9:35*pm, Daniel Norden <dnorden...@gmail.com> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-04-2008
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57