I've been developing a basic clone function
var shallowCopy = function (value) {
// In ES2017, we could also use
// return Object.create(Object.getPrototypeOf(value), Object.getOwnPropertyDescriptors(value));
let propDescriptors = {};
for (let i of Object.getOwnPropertyNames(value)) {
propDescriptors[i] = Object.getOwnPropertyDescriptor(value, i);
}
return Object.create(Object.getPrototypeOf(value), propDescriptors);
};
I have noticed that
Object.prototype.toString.call(shallowCopy([]))
returns [object Object]
instead of [object Array]
.
However, I find the behavior of some new types introduced in ES6 intriguing
Object.prototype.toString.call(shallowCopy(new Set())) // [object Set]
Object.prototype.toString.call(shallowCopy(new Map())) // [object Map]
Does anyone know why these objects exhibit different behavior?
Do you think this behavior will change in the future?
Thank you.