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

A

abozhilov

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.
 
L

Lasse Reichstein Nielsen

Nik said:
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
 
S

Stevo

Lasse said:
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();
 
S

Stevo

Johannes said:
Stevo :


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.
 
L

Lasse Reichstein Nielsen

Stevo said:
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
 
S

Stevo

Lasse said:
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.
 
L

Lasse Reichstein Nielsen

Johannes Baagoe said:
Lasse Reichstein Nielsen :


It seems to be so, but I haven't found the normative reference.


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
 
T

Thomas 'PointedEars' Lahn

Johannes said:
Stevo :

Johannes Baagoe :

Learn to quote.
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
 
D

Dr J R Stockton

In comp.lang.javascript message <S9CdnZnOZNPEvTfXnZ2dnUVZ8oWdnZ2d@gigane
Lasse Reichstein Nielsen :


It seems to be so, but I haven't found the normative reference.


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".
 

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

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top