0

Say I have an IIFE:

let imagodei = {};

;(async function(){
   let C = "12:19";

   imagodei.myIife = aFunctionToGetIifeText()

})(imagodei);

I'd like to define aFunctionToGetIifeText() such that imagodei.myIife is a string of the above code (not including let imagodei = {};). Does anyone know if this is possible?

I've seen these questions, but they apply to the case with a named function:

javascript get function body

How to get function body text in JavaScript?

atomh33ls
  • 26,470
  • 23
  • 104
  • 159

2 Answers2

2

You can use arguments.callee, it is only available on "normal" functions (not arrow functions)

(function() {
  var x = 1;
  console.log(arguments.callee+"");
})()
sney2002
  • 826
  • 2
  • 6
1

Using a deprecated .caller() method -

let imagodei = {};

function aFunctionToGetIifeText(){ 
  console.log(aFunctionToGetIifeText.caller.toString());
}

(async function(){
   let C = "12:19";

   imagodei.myIife = aFunctionToGetIifeText()

})(imagodei);

You can get more reference from this Stackoverflow question

Vandesh
  • 5,508
  • 1
  • 24
  • 31