Skip to content

Instantly share code, notes, and snippets.

@buehler
Last active November 21, 2016 07:22
Show Gist options
  • Save buehler/c36cb5b6cd5106701722d892d14ea985 to your computer and use it in GitHub Desktop.
Save buehler/c36cb5b6cd5106701722d892d14ea985 to your computer and use it in GitHub Desktop.
Default implementation of the ProxyHandler
export class DefaultProxyHandler<T> implements ProxyHandler<T> {
public getPrototypeOf(target: T): any {
return Object.getPrototypeOf(target);
}
public setPrototypeOf(target: T, proto: any): boolean {
return Object.setPrototypeOf(target, proto);
}
public isExtensible(target: T): boolean {
return Object.isExtensible(target);
}
public preventExtensions(target: T): boolean {
Object.preventExtensions(target);
return true;
}
public getOwnPropertyDescriptor(target: T, propertyKey: PropertyKey): PropertyDescriptor {
return Object.getOwnPropertyDescriptor(target, propertyKey);
}
public has(target: T, propertyKey: PropertyKey): boolean {
return target[propertyKey] !== undefined && target[propertyKey] !== null;
}
public get(target: T, propertyKey: PropertyKey, receiver: any): any {
return target[propertyKey];
}
public set(target: T, propertyKey: PropertyKey, value: any, receiver: any): boolean {
target[propertyKey] = value;
return true;
}
public deleteProperty(target: T, propertyKey: PropertyKey): boolean {
return delete target[propertyKey];
}
public defineProperty(target: T, propertyKey: PropertyKey, attributes: PropertyDescriptor): boolean {
return Object.defineProperty(target, propertyKey, attributes);
}
public enumerate(target: T): PropertyKey[] {
return Object.getOwnPropertyNames(target);
}
public ownKeys(target: T): PropertyKey[] {
return Object.getOwnPropertyNames(target);
}
public apply(target: T, thisArg: any, argArray?: any): any {
if (target instanceof Function) {
let func: Function = target as any;
return func.apply(thisArg, argArray);
}
throw new TypeError(`${typeof target} is not a function`);
}
public construct(target: T, thisArg: any, argArray?: any): any {
if (target instanceof Function) {
let func: Function = target as any;
return func.apply(thisArg, argArray);
}
throw new TypeError(`${typeof target} is not a function`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment