javascript - Is it possible to create a map without hardcoding the class name `Map`? -
i tried write utility function returns object same argument. function works fine classes, except es6 collection types such map:
function createinstanceofsameclass(arg) { return object.create(object.getprototypeof(arg)); } const m1 = new map(); const m2 = createinstanceofsameclass(m1); m2.set(1, "x"); // uncaught typeerror: method map.prototype.set called on incompatible receiver #<map>
um... can constructor way:
function create(obj){ return new obj.constructor(); }
Comments
Post a Comment