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?

Lee
  • 29,398
  • 28
  • 117
  • 170

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
  • 856
  • 3
  • 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
  • 6,368
  • 1
  • 26
  • 38