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

Discussion in 'Javascript' started by Matìj Cepl, Nov 24, 2009.

  1. Matìj Cepl

    Matìj Cepl Guest

    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
     
    Matìj Cepl, Nov 24, 2009
    #1
    1. Advertising

  2. On Nov 24, 1:35 pm, Matěj Cepl <> wrote:
    > 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
     
    Dmitry A. Soshnikov, Nov 24, 2009
    #2
    1. Advertising

  3. Matěj Cepl wrote:

    > 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
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
     
    Thomas 'PointedEars' Lahn, Nov 24, 2009
    #3
  4. 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
     
    Matěj Cepl, Nov 24, 2009
    #4
    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. walterbyrd
    Replies:
    1
    Views:
    390
    Damjan
    Apr 10, 2006
  2. Ark Khasin

    Is it just me or just Microsoft?

    Ark Khasin, Jul 15, 2007, in forum: C++
    Replies:
    24
    Views:
    778
    Lionel B
    Jul 16, 2007
  3. Anarki
    Replies:
    1
    Views:
    303
    Alf P. Steinbach
    Aug 12, 2007
  4. nicknameoptional
    Replies:
    2
    Views:
    106
    nicknameoptional
    Jan 24, 2006
  5. Joao Silva
    Replies:
    16
    Views:
    403
    7stud --
    Aug 21, 2009
Loading...

Share This Page