new RegExp().test() or just RegExp().test()

M

Matìj Cepl

Hi,

I have this piece of code

var chosenPair = list.filter(
function (pair) {
return new RegExp(pair.regexp, "i").test(chosingMark);
});

(I hope that particulars of the list data structure are not important
for my question)

I put that new there under the influence of
http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
but I am not sure, whether the same strong need for new is there even
when I just use its .test() method and when (I guess) the object will be
garbage collected almost immediately.

What do you think?

Matìj

--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

Las cosas claras y el chocolate espeso.
(Ideas should be clear and chocolate thick.)
-- Spanish proverb
 
D

Dmitry A. Soshnikov

Hi,

I have this piece of code

var chosenPair = list.filter(
     function (pair) {
         return new RegExp(pair.regexp, "i").test(chosingMark);

});

(I hope that particulars of the list data structure are not important
for my question)

I put that new there under the influence ofhttp://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
but I am not sure, whether the same strong need for new is there even
when I just use its .test() method and when (I guess) the object will be
garbage collected almost immediately.

What do you think?

Matìj

--http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB  25C3 E09F EF25 D964 84AC

Las cosas claras y el chocolate espeso.
(Ideas should be clear and chocolate thick.)
     -- Spanish proverb

It depends on type of build-in constructor. For example, Array or
Function constructors have the same result for being used as a
constructor or called as a function:

// all the same
var a1 = Array(1, 2, 3);
var a2 = new Array(1, 2, 3);
var a3 = [1, 2, 3];

// also the same
var f1 = Function('');
var f2 = new Function('');

Regarding exactly RegExp constructor you should see 15.10.3 and
15.10.4 of ECMA-262-3:

| 15.10.3 The RegExp Constructor Called as a Function:
|
| If pattern is an object R whose [[Class]] property is "RegExp" and
flags is undefined, then return R
| unchanged. Otherwise call the RegExp constructor (15.10.4.1),
passing it the pattern and flags
| arguments and return the object constructed by that constructor.

About the link you gave. There's conceptual difference between
function objects created via expression and via Function constructor.
So the next code from your article is incorrect:

| Do not use new Function to create function values. Use function
expressions instead. For example,
|
| frames[0].onfocus = new Function("document.bgColor='antiquewhite'")
|
| is better written as
|
| frames[0].onfocus = function () {document.bgColor =
'antiquewhite';};

The difference is that [[Scope]] property of functions created via
Function contains only global object. Also such functions are never
take place in optimization with joined objects (see 13.1.2, ES-3).

/ds
 
T

Thomas 'PointedEars' Lahn

Matěj Cepl said:
I have this piece of code

var chosenPair = list.filter(
function (pair) {
return new RegExp(pair.regexp, "i").test(chosingMark);
});

Your indentation is borken.
(I hope that particulars of the list data structure are not important
for my question)

I put that new there under the influence of
http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/

Crockford's unfounded overgeneralizations again. And they have nothing to
do with your question.
but I am not sure, whether the same strong need for new is there even
when I just use its .test() method and when (I guess) the object will be
garbage collected almost immediately.

Guess again. You misunderstand garbage collection.
What do you think?

We've been over this recently. As the outcome is exactly the same (ES3F,
15.10.3.1), it is mostly a matter of style of whether to use `new' or not;
I suppose a person used to Python would have a predilection for omitting
`new', while a person used to Java or PHP would be more likely to write the
`new' here.


PointedEars
 
M

Matěj Cepl

Dne 24.11.2009 14:13, Thomas 'PointedEars' Lahn napsal(a):
We've been over this recently. As the outcome is exactly the same (ES3F,
15.10.3.1), it is mostly a matter of style of whether to use `new' or not;
I suppose a person used to Python would have a predilection for omitting
`new', while a person used to Java or PHP would be more likely to write the
`new' here.

Hmm, that's probably it ... I am a Pythonista by heart, using Javascript
because I need to.

Thanks,

Matěj
--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

Experience is what you get when you don't get what you want.
-- Dan Stanford
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top