johnlowvale johnlowvale - 6 months ago 39
Javascript Question

JavaScript Object.assign not working on Date object

Regular objects can be cloned using this method:

a = {x:9}; //sample
b = Object.assign(Object.create(a),a);

However, the variables of Date type don't seem to work with Object.assign and Object.create:

a = new Date();
b = Object.assign(Object.create(a),a);

Results of printing a, b are not the same:
Thu Oct 20 2016 11:17:29 GMT+0700 (SE Asia Standard Time)
Date {}

I know I can create a clone of Date object another way using

b = new Date(a)

But why are Object.assign and Object.create not working on Date type?


The Object.assign() method copies over the enumerable and own properties of the source object. A Date instance doesn't really have any of those (if you don't add any with your own code).

In particular, Date "properties" like the year, month, date, etc. aren't properties in the JavaScript sense. They're values that can be retrieved via the API. That doesn't make them properties.