Created
December 18, 2020 03:46
-
-
Save oscar60310/7e0e46e3c2d5ed0f173ca72fea738ec2 to your computer and use it in GitHub Desktop.
Snippet about typescript decorators this binding
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
const authorize = () => ( | |
target: any, | |
propertyKey: string, | |
) => { | |
let next: any; | |
const getter = () => next; | |
const setter = (old: any) => { | |
next = () => { | |
return old() +1; | |
} | |
}; | |
Object.defineProperty(target, propertyKey, { | |
get: getter, | |
set: setter, | |
configurable: true, | |
enumerable: true, | |
}); | |
}; | |
const authorizeP = () => ( | |
target: any, | |
propertyKey: string, | |
descriptor: any | |
) => { | |
const originalMethod = descriptor.value; | |
descriptor.value = function() { | |
return originalMethod.apply(this) + 1; | |
}; | |
}; | |
class A { | |
public b =3; | |
@authorizeP() | |
@authorizeP() | |
public m (){ | |
console.log(this.b); | |
return 1; | |
} | |
} | |
const a = new A(); | |
a.b = 5; | |
console.log(a.m()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment