javascript - Deep Freeze any type -
is enough deep freeze kind of type?
function freeze(obj) { if (typeof obj === 'object') { reflect.ownkeys(obj).foreach((key) => {freeze(obj[key])}); } return object.freeze(obj); }
no. unfortunately, javascript still doesn't have way that. example, date
instances cannot frozen:
var dt = new date(2016, 11, 27); console.log(dt.getdate()); // 27 object.freeze(dt); dt.setdate(dt.getdate() + 1); console.log(dt.getdate()); // 28
some other issues/notes:
- as matÃas said, functions objects,
typeof
give"function"
them. reflect.ownkeys
gives object's own keys, not keys prototypes. depending on definition of "deep freeze," may need copy inherited properties object before freezing it.
Comments
Post a Comment