niim
Version:
Node Inspect IMproved
39 lines (32 loc) • 840 B
JavaScript
const real = [1,2,3,4,5,6,7,8,9, {hello: 'world'}];
function arrClone(arr) {
return new Proxy([].concat(arr), {
get: function(target, prop) {
if (typeof prop === 'symbol')
return target[prop];
switch(typeof target[prop]) {
case 'undefined':
case 'number':
case 'string':
case 'boolean':
case 'function':
case 'symbol':
return target[prop]; /* immutable */
case 'object':
return {...target[prop]};
default:
throw new Error('I have no idea what to do with', `(${prop})`);
}
},
set: function(target, prop, value) {
target[prop] = value;
return true;
}
});
}
var px = arrClone(real);
console.log(px.filter((x) => x > 3));
console.log(px.splice(1,2));
console.log(px);
console.log(real);