Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
¿Cómo encuentra la función de llamada en JavaScript cuando está habilitado el uso estricto?

¿Es posible ver la persona que llama/llama de una función cuando está habilitado el use strict ?

 'use strict'; function jamie (){ console.info(arguments.callee.caller.name); //this will output the below error //uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them }; function jiminyCricket (){ jamie(); } jiminyCricket ();

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Por lo que vale, estoy de acuerdo con los comentarios anteriores. Para cualquier problema que intente resolver, generalmente hay mejores soluciones.

Sin embargo, solo con fines ilustrativos, aquí hay una solución ( muy fea ):

 'use strict' function jamie (){ var callerName; try { throw new Error(); } catch (e) { var re = /(\w+)@|at (\w+) \(/g, st = e.stack, m; re.exec(st), m = re.exec(st); callerName = m[1] || m[2]; } console.log(callerName); }; function jiminyCricket (){ jamie(); } jiminyCricket(); // jiminyCricket

Solo probé esto en Chrome, Firefox e IE11, por lo que su kilometraje puede variar.

over 4 years ago · Santiago Trujillo Report

0

Tenga en cuenta que esto no debe usarse en producción. Esta es una solución fea, que puede ser útil para la depuración, pero si necesita algo de la persona que llama, páselo como argumento o guárdelo en una variable accesible.

La versión corta de la respuesta de @pswg (sin arrojar un error, solo instanciando uno):

 let re = /([^(]+)@|at ([^(]+) \(/g; let aRegexResult = re.exec(new Error().stack); sCallerName = aRegexResult[1] || aRegexResult[2];

Fragmento completo:

 'use strict' function jamie (){ var sCallerName; { let re = /([^(]+)@|at ([^(]+) \(/g; let aRegexResult = re.exec(new Error().stack); sCallerName = aRegexResult[1] || aRegexResult[2]; } console.log(sCallerName); }; function jiminyCricket(){ jamie(); }; jiminyCricket(); // jiminyCricket

over 4 years ago · Santiago Trujillo Report

0

No me funcionó Esto es lo que finalmente hago, por si a alguien le sirve

 function callerName() { try { throw new Error(); } catch (e) { try { return e.stack.split('at ')[3].split(' ')[0]; } catch (e) { return ''; } } } function currentFunction(){ let whoCallMe = callerName(); console.log(whoCallMe); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!