我需要将空对象转换为Date对象,如下所示:
let dest = {};
let src = new Date();
someFunctionToPushSrcIntoDest(dest, src);
dest instanceof Date // should return true
dest.setTime(0) // and all other methods fo Date should work as expected
我尝试了几种解决方案。我的第一次尝试是使用lodash'extend
let dest = {};
let src = new Date();
_.extend(dest, src);
// but...
typeof dest === 'object' // true
dest instanceof Date // false
然后我尝试了
let dest = {};
let src = new Date();
Object.setPrototype(dest, Object.getPrototype(src));
// but...
typeof dest === 'object'; // true
dest instanceof Date; // true - Good !!!
dest.getTime(); // Uncaught TypeError: this is not a Date object...
dest.setTime(); // Uncaught TypeError: Method Date.prototype.setTime called on incompatible receiver [object Object]
然后我尝试了Object.assign
和其他各种排列,但我找不到将Date克隆到现有对象实例的方法。我在尝试一些不可能的事吗任何帮助赞赏!感谢。