Skip to content

Instantly share code, notes, and snippets.

@fabiospampinato
Created April 3, 2024 15:16
Show Gist options
  • Save fabiospampinato/90fcc0f2d188ced532bce947de974575 to your computer and use it in GitHub Desktop.
Save fabiospampinato/90fcc0f2d188ced532bce947de974575 to your computer and use it in GitHub Desktop.
Setting Array.prototype.length doesn't explicitly tell you which properties got deleted
const proxy = new Proxy ( [0, 1, 2], {
get: ( ...args ) => {
console.log ( '[get]', ...args );
return Reflect.get ( ...args );
},
apply: ( ...args ) => {
console.log ( '[apply]', ...args );
return Reflect.apply ( ...args );
},
construct: ( ...args ) => {
console.log ( '[construct]', ...args );
return Reflect.construct ( ...args );
},
defineProperty: ( ...args ) => {
console.log ( '[defineProperty]', ...args );
return Reflect.defineProperty ( ...args );
},
deleteProperty: ( ...args ) => {
console.log ( '[deleteProperty]', ...args );
return Reflect.deleteProperty ( ...args );
},
getOwnPropertyDescriptor: ( ...args ) => {
console.log ( '[getOwnPropertyDescriptor]', ...args );
return Reflect.getOwnPropertyDescriptor ( ...args );
},
getPrototypeOf: ( ...args ) => {
console.log ( '[getPrototypeOf]', ...args );
return Reflect.getPrototypeOf ( ...args );
},
has: ( ...args ) => {
console.log ( '[has]', ...args );
return Reflect.has ( ...args );
},
isExtensible: ( ...args ) => {
console.log ( '[isExtensible]', ...args );
return Reflect.isExtensible ( ...args );
},
ownKeys: ( ...args ) => {
console.log ( '[ownKeys]', ...args );
return Reflect.ownKeys ( ...args );
},
preventExtensions: ( ...args ) => {
console.log ( '[preventExtensions]', ...args );
return Reflect.preventExtensions ( ...args );
},
set: ( ...args ) => {
console.log ( '[set]', ...args );
return Reflect.set ( ...args );
},
setPrototypeOf: ( ...args ) => {
console.log ( '[setPrototypeOf]', ...args );
return Reflect.setPrototypeOf ( ...args );
}
});
proxy.length = 0;
// Output:
// [set] [ 0, 1, 2 ] length 0 [ 0, 1, 2 ]
// [getOwnPropertyDescriptor] [ 0, 1, 2 ] length
// [defineProperty] [ 0, 1, 2 ] length { value: 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment