0

Impossible to get the this of caller function | Constructor.

It seems : NodeJS , Client-Side(Firefox,....) are avoid arguments.callee.caller with ES6 ,

Any solution to this problem ,known that ,

my ES5 code is : ✓

function setProp(prop,val){
    arguments.callee.caller.prototype[prop]=val;
}

function Person(){
   setProp('birthday',new Date());
   return this;
}
    

Wanted ES6 : ✘

function setProp(prop,val){
   arguments.callee.caller.prototype[prop]=val;  //--- Throw ERROR
   //Another attempt : arguments.callee(true).caller[prop]=val //--- throw Error
}
class Person{

   constructor(){
      setProp('birthday',new Date())
   }
} 

We try to convert setProp to arrow-function . however this means window and arguments is undefined there

Community
  • 1
  • 1
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • 3
    Have you considered not using antipatterns like this? – ssube Jul 19 '16 at 15:50
  • 1
    This feature was deprecated in ES5 when "strict mode" was added. Most transpilers treat standalone files as ES6 modules, and they are automatically strict. If you want to use this, you'd need to set up your builds to not expect ES6 modules. Really though, you should be explicitly passing in whatever arguments you need. Your code's only going to get more unmaintainable if you's using `.callee` and `.caller`. – loganfsmyth Jul 19 '16 at 16:41
  • 1
    `callee` has long been deprecated, `caller` never even was part of the standard! _"Impossible to get the this of caller function"_ `setProp` doesn't use that anyway. Whenever you create an instance of `Person` its prototype is changed. What's the point of that? And why don't you just pass it to the function? – a better oliver Jul 20 '16 at 07:42

0 Answers0