473,320 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

'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(RegExp("\\s*\\b" + name + "\\b\\s*"), " ");
Thanks,
Dano
Nov 3 '08 #1
11 1161
Daniel Norden <dn********@gmail.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(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.'
Nov 3 '08 #2
On Nov 4, 8:04*am, Daniel Norden <dnorden...@gmail.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(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
Nov 3 '08 #3
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
Nov 3 '08 #4
Daniel Norden <dn********@gmail.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
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/
Nov 3 '08 #5
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
Nov 3 '08 #6
On Nov 3, 6:24*pm, Daniel Norden <dnorden...@gmail.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.
Nov 4 '08 #7
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
Nov 4 '08 #8
On Nov 3, 9:35*pm, Daniel Norden <dnorden...@gmail.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.
Nov 4 '08 #9
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>
Nov 4 '08 #10
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, <f8*******************@news.demon.co.uk>
Nov 4 '08 #11
In comp.lang.javascript message <49**************@PointedEars.de>, Tue,
4 Nov 2008 08:57:59, Thomas 'PointedEars' Lahn <Po*********@web.de>
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.
Nov 4 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: skscpp | last post by:
I have a question about multiple inheritance, operator new/delete and ambiguity. Here is some code that I wrote to analyze this problem. // test.h ========================== #include <new>...
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
6
by: Jay Nabonne | last post by:
Hi, This might sound odd, but we want to replace the allocation scheme used by new and delete without changing operator new and operator delete. (The global operators are shared and we can't...
6
by: Vinu | last post by:
Hi, I am maintaining a C++ project which is a server which continuously receives requeste from clients. I have noticed that we overload the new operator and in it then call malloc to allocate...
3
by: jacob navia | last post by:
Hi I am implementing operator overloading in the context of the C language. I would like to have your opinion about this: Would it be a good idea to assume that the operator != could be...
1
by: Alex Fennell | last post by:
Hi all, I am wanting to write a memory allocation statistic gathering wrapper and here is a basic bit of code that I wrote to back it up: class CMemoryManager { public: static void*...
13
by: Alex Vinokur | last post by:
Is the any difference between ------ 1 ------ Foo* p = operator new (n * sizeof (Foo)); // Stuff delete p; --------------- and
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.