Created
June 21, 2019 08:02
-
-
Save 197291/ce61befac35c5cfb628cc89900905fc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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