Bind the function to yourself (taking a hint from the answers of @ArunPJohny and @BudgieInWA):
crazy = crazy.bind(crazy);
This will give you access from the function to its properties through this .
> crazy() function () { console.log(this); console.log(this.isCrazy);
This seems like a better solution than the accepted answer, which uses the callee function, which is deprecated and does not work in strict mode.
You can also now call a function call recursively with this() if you were so prone.
We will call it self-determining . Write a small useful feature:
function selfthisify(fn) { return fn.bind(fn); } crazy = selfthisify(crazy); crazy();
Or, if you prefer more “semantic" names, you can call it accessOwnProps .
If you are a syntactic type of sugar type, you can add the selfthisify property to the function prototype:
Object.defineProperty(Function.prototype, 'selfthisify', { get: function() { return this.bind(this); } });
Now you can say
crazy.selfthisify();
user663031 Oct 16 '14 at 13:16 2014-10-16 13:16
source share