Your examples are two different things. The first example creates three separate Date objects, which can have three different dates.
The second example creates one Date object, which var1, var2, and var3 point to. When you change the Date object, var1, var2, and var3 will all see that change.
Answer from Edwin Torres on Stack OverflowVideos
Your examples are two different things. The first example creates three separate Date objects, which can have three different dates.
The second example creates one Date object, which var1, var2, and var3 point to. When you change the Date object, var1, var2, and var3 will all see that change.
new Date is a constructor. it is written in javascript. In OO programming you usually don't replicate code. So if you have a constructor new Date() - which you have in JS, and setters, it is most likely that doing new Date(2013, 2, 30) is equivalent to:
var myDate = new Date();
myDate.setDate(30);
myDate.setMonth(2);
myDate.setYear(2013);
according to this, the allocation alone will cost you and the second way you presented is faster. BUT (!!!) the two ways don't give the same results. In the first you gain three different objects. in the later you get only one.
var d = new Date(2011,10,30);
as months are indexed from 0 in js.
You definitely want to use the second expression since months in JS are enumerated from 0.
Also you may use Date.parse method, but it uses different date format:
var timestamp = Date.parse("11/30/2011");
var dateObject = new Date(timestamp);
From the specs:
When
Dateis called as a function rather than as a constructor, it returns a String representing the current time (UTC).
and:
When
Dateis called as part of anewexpression, it is a constructor: it initialises the newly created object.
So, new Date(...) returns an object such that obj instanceof Date is true, whereas Date(...) basically returns the same as new Date().toString().
new Date creates a new Date object that you can modify or initialize with a different date while Date returns a string of the current date/time, ignoring its arguments.
The first example results in an object with keys based on the user's locale's string representation for dates, each containing the same value as a Date object. This is not particularly useful, in part because the keys will differ depending on the user's locale, and because in order to access the value of one of those keys, you would need to know the date that matches it... which is the value you'd be looking up in the first place.
The second example results in an empty object (because it just references each "key", without assigning a value to it.)
Copyvar eventDates = {};
eventDates[new Date('10/04/2017')] = new Date('10/04/2017');
eventDates[new Date('10/06/2017')] = new Date('10/06/2017');
eventDates[new Date('10/20/2017')] = new Date('10/20/2017');
eventDates[new Date('10/26/2017')] = new Date('10/26/2017');
console.log(eventDates);
var eventDates2 = {};
eventDates2[new Date('10/04/2017')];
eventDates2[new Date('10/06/2017')];
eventDates2[new Date('10/20/2017')];
eventDates2[new Date('10/26/2017')];
console.log(eventDates2)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
It's not clear to me what the author of either of these examples was trying to accomplish. If you really wanted an array of dates, you would instead do this:
Copyvar eventDatesArray = [];
eventDatesArray.push(new Date('10/04/2017'));
eventDatesArray.push(new Date('10/06/2017'));
eventDatesArray.push(new Date('10/20/2017'));
eventDatesArray.push(new Date('10/26/2017'));
console.log(eventDatesArray);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
It'not the same.
In the first array you are setting the new Date object to be the key and the value of the array.
In the second you are setting just the key.
example:
Lets say you have an array , array = {};
If you wanna set the key of the array you do it like this.
Copyarray['key1'] = someValue1;
array['key2'] = someValue2;
array['key3'] = someValue3;
And then you can get these values with each key.
Copyvar variable1 = array['key1'];
Now if you console.log this variable1 you get someValue1
Copylet array1 = {};
array1['key1'] = 1;
array1['key2'] = 2;
array1['key2'] = 3;
let array2 = {};
array2['key1'];
array2['key2'];
array2['key2'];
console.log('First Array',array1['key1']);
console.log('First Array',array2['key1']);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
That's the + unary operator. It's equivalent to:
function(){ return Number(new Date); }
See http://xkr.us/articles/javascript/unary-add and MDN.
JavaScript is loosely typed, so it performs type coercion/conversion in certain circumstances:
http://blog.jeremymartin.name/2008/03/understanding-loose-typing-in.html
http://www.jibbering.com/faq/faq_notes/type_convert.html
Other examples:
>>> +new Date()
1224589625406
>>> +"3"
3
>>> +true
1
>>> 3 == "3"
true