Is there a difference between +new Date() and new Date()?

Discussion in 'Javascript' started by Nik, Sep 11, 2009.

  1. Nik

    Nik Guest

    As title and what is the difference?

    THANKS!!!!!!!!!
    Nik, Sep 11, 2009
    #1
    1. Advertising

  2. Nik

    eirc Guest

    On Sep 11, 12:39 pm, Nik <> wrote:
    > As title and what is the difference?
    >
    > THANKS!!!!!!!!!


    i don't think there has difference,maybe +new Date() is a expression,
    new Date() is a object
    eirc, Sep 11, 2009
    #2
    1. Advertising

  3. Nik

    abozhilov Guest

    On Sep 11, 8:29 am, eirc <> wrote:
    > i don't think there has difference,maybe +new Date() is a expression,
    > new Date() is a object


    In first case, he convert type to primitive number value. JavaScript
    automatic call
    new Date().valueOf();

    In second construction, he initialize `object` whos [[Prototype]]
    refer to Date.prototype.
    abozhilov, Sep 11, 2009
    #3
  4. Nik <> writes:

    > As title and what is the difference?


    Please repeat the question in the body of the message. It breaks
    readability to have to refer to the subject while reading the message.

    new Date() creates a Date object.
    The unary + (positive sign) operation converts its operand to a
    primitive numeric value. For a Date object that is equivalent to
    calling valueOf (which is againg equivalent to calling getTime)
    on the object.

    I.e., one is a number, the other is an object.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Sep 11, 2009
    #4
  5. Nik

    Stevo Guest

    Lasse Reichstein Nielsen wrote:
    > Nik <> writes:
    >
    >> As title and what is the difference?

    >
    > Please repeat the question in the body of the message. It breaks
    > readability to have to refer to the subject while reading the message.
    >
    > new Date() creates a Date object.
    > The unary + (positive sign) operation converts its operand to a
    > primitive numeric value. For a Date object that is equivalent to
    > calling valueOf (which is againg equivalent to calling getTime)
    > on the object.
    >
    > I.e., one is a number, the other is an object.
    >
    > /L


    So I can optimize some code I have that looks currently like this:

    var start=new Date().getTime();

    I could optimize it to this:

    var start=0+new Date();
    Stevo, Sep 11, 2009
    #5
  6. Nik

    Stevo Guest

    Johannes Baagoe wrote:
    > Stevo :
    >
    >> So I can optimize some code I have that looks currently like this:
    >>
    >> var start=new Date().getTime();
    >>
    >> I could optimize it to this:
    >>
    >> var start=0+new Date();

    >
    > Yes, you could even omit the zero.
    >
    > Whether it is an optimisation is debatable. A gain of speed is unlikely.
    > It does save typing, but as your question and the OP's show, at some
    > expense of clarity.


    Yes it reduces clarity and is unlikely to give a speed gain. I'm
    thinking only in terms of file size. If I've got 20 of them then there's
    200 bytes saved. If I can manage 5 similar sized optimizations on a 20K
    file I'm getting a nice file size reduction out of it.
    Stevo, Sep 11, 2009
    #6
  7. Stevo <> writes:

    > Yes it reduces clarity and is unlikely to give a speed gain. I'm
    > thinking only in terms of file size. If I've got 20 of them then
    > there's 200 bytes saved. If I can manage 5 similar sized optimizations
    > on a 20K file I'm getting a nice file size reduction out of it.


    If you have 20 of anything, you should introduce a new function intead,
    and call that 20 times. If the function name is shorter than "+new Date",
    it might even save space.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Sep 11, 2009
    #7
  8. Nik

    Stevo Guest

    Lasse Reichstein Nielsen wrote:
    > Stevo <> writes:
    >
    >> Yes it reduces clarity and is unlikely to give a speed gain. I'm
    >> thinking only in terms of file size. If I've got 20 of them then
    >> there's 200 bytes saved. If I can manage 5 similar sized optimizations
    >> on a 20K file I'm getting a nice file size reduction out of it.

    >
    > If you have 20 of anything, you should introduce a new function intead,
    > and call that 20 times. If the function name is shorter than "+new Date",
    > it might even save space.
    >
    > /L


    Unfortunately a function wouldn't be shorted unless it was a one or two
    letter named function, and I don't want one of them in the global
    namespace, so it would have to be a method of an object which makes
    addressing it even longer. It doesn't end up being viable.
    Stevo, Sep 11, 2009
    #8
  9. Johannes Baagoe <> writes:

    > Lasse Reichstein Nielsen :
    >
    >> The unary + (positive sign) operation converts its operand to a
    >> primitive numeric value. For a Date object that is equivalent to
    >> calling valueOf

    >
    > It seems to be so, but I haven't found the normative reference.
    >
    >> (which is againg equivalent to calling getTime) on the object.

    >
    > In the two implementations I have checked (SpiderMonkey and V8),
    > if you override Date.prototype.getTime, the automatic conversion
    > to a numeric value remains unchanged. Not so if you override valueOf.


    That is correct. The equivalence only holds for unmodified Date
    objects. They both return the same value, but only valueOf is used
    in the default conversion to number.

    The normative references to the ECMAScript spec are:

    The unary plus operator converts its operand to a number using the
    ToNumber function (11.4.6).
    The operand is a Date object, which is an object, so ToNumber
    first calls ToPrimtive on the object with hint Number, and then
    calls ToNumber on the result of that (9.3).
    ToPrimitive calls the object's [[DefaultValue]] function with
    the hint Number (9.1).
    The [[DefaultValue]] function with hint Number calls the valueOf
    function on the object, and returns the result if it's a primitive
    (8.6.2.6).
    The valueOf function of a Date object returns the time value
    of the Date object (15.9.5.8). This is a number.
    This means [[DefaultValue]] returns the same value, and so does
    ToPrimitive. Then ToNumber returns ToNumber of this value, which
    is the value again (9.3)
    This is the value returned by the unary plus operator.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Sep 11, 2009
    #9
  10. Johannes Baagoe wrote:
    > Stevo :
    >>> So I can optimize some code I have that looks currently like this:

    >>
    >>> var start=new Date().getTime();

    >>
    >>> I could optimize it to this:

    >>
    >>> var start=0+new Date();

    >
    > Johannes Baagoe :


    Learn to quote.

    >> Yes, you could even omit the zero.

    >
    > Oops, sorry. No, you *must* omit the zero. Otherwise, start will be
    > something like "0Fri Sep 11 2009 20:20:25 GMT+0200 (CEST)".


    ACK. That is because for a Date object referred to by `d' the addition
    operator calls ToPrimitive(d) which calls (d).[[DefaultValue]]() which works
    like d.[[DefaultValue]](PreferredType=String) which returns d.toString().
    See ES3F, sections 11.6.1, 9.1, 8.6.2.6, and 15.9.5.2.

    As a result, the number value 0 is converted to string, and string
    concatenation takes place (ES3F, section 16.1, steps 7 to 15.)


    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, Sep 11, 2009
    #10
  11. [OT] Quoting (was: Quoting)

    Johannes Baagoe wrote:
    > Thomas 'PointedEars' Lahn :
    >> Learn to quote.

    >
    > To take the canonical example (e.g.,
    > http://www.anta.net/misc/nnq/nquote.shtml#Q6) :
    >
    > In which style is it easier to attribute what to whom [...]


    You are asking the wrong question. Ask yourself instead, in which
    style it would be easier to quote an already quoted posting.

    That aside, when in Rome ...


    F'up2 poster

    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> (404-comp.)
    Thomas 'PointedEars' Lahn, Sep 11, 2009
    #11
  12. In comp.lang.javascript message <S9CdnZnOZNPEvTfXnZ2dnUVZ8oWdnZ2d@gigane
    ws.com>, Fri, 11 Sep 2009 05:03:05, Johannes Baagoe <>
    posted:
    >Lasse Reichstein Nielsen :
    >
    >> The unary + (positive sign) operation converts its operand to a
    >> primitive numeric value. For a Date object that is equivalent to
    >> calling valueOf

    >
    >It seems to be so, but I haven't found the normative reference.
    >
    >> (which is againg equivalent to calling getTime) on the object.

    >
    >In the two implementations I have checked (SpiderMonkey and V8),
    >if you override Date.prototype.getTime, the automatic conversion
    >to a numeric value remains unchanged. Not so if you override valueOf.


    That is as it should be. Unary + calls for valueOf, if not already
    numeric. Method getTime should never have been introduced :
    (1) It is superfluous
    (2) What it gets is not what is commonly called the time.

    ISTM that all Objects that can support valueOf (& IIRC that's all of
    them) should support setValue (and valueOf should have been spelt
    getValue).




    To another article by you :
    >Yes, you could even omit the zero.


    And he should.


    Unary + and - are worth learning. It's easy to do so, since one learned
    to use them with digit strings at an early age. Their general
    interpretation is AFAICS the only plausible one; for JavaScript, one
    just needs to know what they can be applied to. They can in fact be
    applied to any thing, but not always usefully.

    I cannot think of a use for unary * (except as a mere alternative to
    unary +); unary / could mean "reciprocal", but that does not seem
    beneficial on the whole. If \ was integer division, then postfix unary
    \ could be "truncate towards zero".

    --
    (c) John Stockton, near London. *@merlyn.demon.co.uk/?.?
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
    Dr J R Stockton, Sep 11, 2009
    #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. jakk
    Replies:
    4
    Views:
    12,057
  2. Mario Fratelli
    Replies:
    32
    Views:
    1,358
    Phlip
    Dec 9, 2003
  3. Santa
    Replies:
    1
    Views:
    1,062
    Mark A. Odell
    Jul 17, 2003
  4. PerlFAQ Server
    Replies:
    0
    Views:
    346
    PerlFAQ Server
    Jan 6, 2011
  5. Xu, Qian
    Replies:
    2
    Views:
    75
Loading...

Share This Page