Skip to content

Instantly share code, notes, and snippets.

@197291
Created June 21, 2019 08:02
Show Gist options
  • Save 197291/ce61befac35c5cfb628cc89900905fc2 to your computer and use it in GitHub Desktop.
Save 197291/ce61befac35c5cfb628cc89900905fc2 to your computer and use it in GitHub Desktop.
export function logClass(target: Function) {
// Save link for original constructor
const original = target;
// function generate instance of class
function construct(constructor, args) {
const c: any = function () {
return constructor.apply(this, args);
}
c.prototype = constructor.prototype;
return new c();
}
// definition of behavior for new construtor
const f: any = function (...args) {
console.log(`New: ${original['name']} is created`);
//New: Employee создан
return construct(original, args);
}
// Copying prototype in order to instanceOf works
f.prototype = original.prototype;
// Return new constructor which overwright original constructor
return f;
}
@logClass
class Employee {
}
let emp = new Employee();
console.log('emp instanceof Employee');
//emp instanceof Employee
console.log(emp instanceof Employee);
//true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment