IT story

JavaScript에서 현재 실행중인 함수의 이름을 얻을 수 있습니까?

hot-time 2020. 5. 26. 07:46
반응형

JavaScript에서 현재 실행중인 함수의 이름을 얻을 수 있습니까?


이것을 할 수 있습니까?

myfile.js:
function foo() {
    alert(<my-function-name>);
    // pops-up "foo"
    // or even better: "myfile.js : foo"
}

내 스택에 Dojo 및 jQuery 프레임 워크가 있으므로 둘 중 하나가 더 쉬워지면 사용할 수 있습니다.


을 사용하여 얻을 수 있어야합니다 arguments.callee.

추가 정크가 포함되어 있으므로 이름을 구문 분석해야 할 수도 있습니다. 그러나 일부 구현에서는 간단히을 사용하여 이름을 얻을 수 있습니다 arguments.callee.name.

파싱 ​​:

function DisplayMyName() 
{
   var myName = arguments.callee.toString();
   myName = myName.substr('function '.length);
   myName = myName.substr(0, myName.indexOf('('));

   alert(myName);
}

출처 : Javascript-현재 함수 이름을 가져옵니다 .


비 익명 함수

function foo()
{ 
    alert(arguments.callee.name)
}

그러나 오류 처리기의 경우 결과는 오류 처리기 함수의 이름이됩니까?


필요한 것은 간단합니다. 함수 만들기 :

function getFuncName() {
   return getFuncName.caller.name
}

그런 다음 필요할 때마다 다음을 사용하십시오.

function foo() { 
  console.log(getFuncName())
}

foo() 
// Logs: "foo"

MDN 에 따르면

경고 : ECMAScript (ES5) 5 판에서는 엄격 모드에서 arguments.callee ()를 사용할 수 없습니다. 함수 표현식에 이름을 지정하거나 함수 자체를 호출해야하는 함수 선언을 사용하여 arguments.callee ()를 사용하지 마십시오.

언급했듯이 이것은 스크립트가 "엄격 모드"를 사용하는 경우 에만 적용 됩니다 . 이것은 주로 보안상의 이유이며 슬프게도 현재로서는 이에 대한 대안이 없습니다.


이것은해야합니다 :

var fn = arguments.callee.toString().match(/function\s+([^\s\(]+)/);
alert(fn[1]);

발신자의 경우을 사용하십시오 caller.toString().


이것은 "세계에서 가장 추악한 해킹"의 범주로 가야하지만 여기 있습니다.

먼저, 현재 함수 의 이름을 인쇄하는 것은 (다른 답변에서와 같이) 나에게 제한적 인 것처럼 보입니다. 기능이 무엇인지 이미 알고 있기 때문입니다!

그러나 호출 함수 의 이름을 찾는 것은 추적 함수에 매우 유용 할 수 있습니다. 이것은 정규 표현식을 사용하지만 indexOf를 사용하면 약 3 배 빠릅니다.

function getFunctionName() {
    var re = /function (.*?)\(/
    var s = getFunctionName.caller.toString();
    var m = re.exec( s )
    return m[1];
}

function me() {
    console.log( getFunctionName() );
}

me();

작동하는 방법은 다음과 같습니다.

export function getFunctionCallerName (){
  // gets the text between whitespace for second part of stacktrace
  return (new Error()).stack.match(/at (\S+)/g)[1].slice(3);
}

그런 다음 테스트에서 :

import { expect } from 'chai';
import { getFunctionCallerName } from '../../../lib/util/functions';

describe('Testing caller name', () => {

    it('should return the name of the function', () => {
      function getThisName(){
        return getFunctionCallerName();
      }

      const functionName = getThisName();

      expect(functionName).to.equal('getThisName');
    });

  it('should work with an anonymous function', () => {


    const anonymousFn = function (){
      return getFunctionCallerName();
    };

    const functionName = anonymousFn();

    expect(functionName).to.equal('anonymousFn');
  });

  it('should work with an anonymous function', () => {
    const fnName = (function (){
      return getFunctionCallerName();
    })();

    expect(/\/util\/functions\.js/.test(fnName)).to.eql(true);
  });

});

세 번째 테스트는 테스트가 / util / functions에있는 경우에만 작동합니다.


다른 유스 케이스는 런타임에 바인드 된 이벤트 디스패처 일 수 있습니다.

MyClass = function () {
  this.events = {};

  // Fire up an event (most probably from inside an instance method)
  this.OnFirstRun();

  // Fire up other event (most probably from inside an instance method)
  this.OnLastRun();

}

MyClass.prototype.dispatchEvents = function () {
  var EventStack=this.events[GetFunctionName()], i=EventStack.length-1;

  do EventStack[i]();
  while (i--);
}

MyClass.prototype.setEvent = function (event, callback) {
  this.events[event] = [];
  this.events[event].push(callback);
  this["On"+event] = this.dispatchEvents;
}

MyObject = new MyClass();
MyObject.setEvent ("FirstRun", somecallback);
MyObject.setEvent ("FirstRun", someothercallback);
MyObject.setEvent ("LastRun", yetanothercallback);

여기서 장점은 디스패처를 쉽게 재사용 할 수 있고 디스패치 큐를 인수로 수신 할 필요가 없으며 대신 호출 이름으로 암시됩니다.

결국 여기에 제시된 일반적인 경우는 "함수 이름을 인수로 사용하여 명시 적으로 전달할 필요가 없습니다"이며, jquery animate () 선택적 콜백과 같은 많은 경우에 유용 할 수 있습니다. 또는 타임 아웃 / 간격 콜백에서 (즉, 함수 NAME 만 전달).


getMyName아래 스 니펫 함수는 호출 함수의 이름을 반환합니다. 그것은 해킹이며 비표준 기능 에 의존 합니다 : Error.prototype.stack. 에 의해 반환되는 문자열 형식은 Error.prototype.stack엔진마다 다르게 구현되므로 다음과 같은 경우에는 작동하지 않을 수 있습니다.

function getMyName() {
  var e = new Error('dummy');
  var stack = e.stack
                .split('\n')[2]
                // " at functionName ( ..." => "functionName"
                .replace(/^\s+at\s+(.+?)\s.+/g, '$1' );
                return stack
}

function foo(){
  return getMyName()
}

function bar() {
  return foo()
}

console.log(bar())

다른 솔루션 소개 : arguments.callee 엄격 모드에서 허용되지 않습니다 하고 Function.prototype.caller있다 비표준 및 엄격한 모드에서 사용할 수 없습니다 .


이름이 지정된 함수를 작성했기 때문에 foo당신은 그것을에 알고 myfile.js동적으로이 정보를 얻을 필요가 왜?

그것은 arguments.callee.toString()함수 내부에서 사용할 수 있으며 (이것은 전체 함수의 문자열 표현입니다) 함수 이름의 값을 정규식으로 계산하십시오.

다음은 자체 이름을 뱉어내는 함수입니다.

function foo() {
    re = /^function\s+([^(]+)/
    alert(re.exec(arguments.callee.toString())[1]);             
}

이에 대한 업데이트 된 답변은 다음 답변에서 확인할 수 있습니다. https://stackoverflow.com/a/2161470/632495

클릭이 마음에 들지 않으면

function test() {
  var z = arguments.callee.name;
  console.log(z);
}

정보는 2016 년에 실제입니다.


함수 선언 결과

오페라 결과

>>> (function func11 (){
...     console.log(
...         'Function name:',
...         arguments.callee.toString().match(/function\s+([_\w]+)/)[1])
... })();
... 
... (function func12 (){
...     console.log('Function name:', arguments.callee.name)
... })();
Function name:, func11
Function name:, func12

크롬 결과

(function func11 (){
    console.log(
        'Function name:',
        arguments.callee.toString().match(/function\s+([_\w]+)/)[1])
})();

(function func12 (){
    console.log('Function name:', arguments.callee.name)
})();
Function name: func11
Function name: func12

NodeJS의 결과

> (function func11 (){
...     console.log(
.....         'Function name:',
.....         arguments.callee.toString().match(/function\s+([_\w]+)/)[1])
... })();
Function name: func11
undefined
> (function func12 (){
...     console.log('Function name:', arguments.callee.name)
... })();
Function name: func12

Firefox에서는 작동하지 않습니다. IE와 Edge에서 테스트되지 않았습니다.


함수 표현식의 결과

NodeJS의 결과

> var func11 = function(){
...     console.log('Function name:', arguments.callee.name)
... }; func11();
Function name: func11

크롬 결과

var func11 = function(){
    console.log('Function name:', arguments.callee.name)
}; func11();
Function name: func11

Firefox, Opera에서는 작동하지 않습니다. IE와 Edge에서 테스트되지 않았습니다.

노트:

  1. Anonymous function does not to make sense to check.
  2. Testing environment

~ $ google-chrome --version
Google Chrome 53.0.2785.116           
~ $ opera --version
Opera 12.16 Build 1860 for Linux x86_64.
~ $ firefox --version
Mozilla Firefox 49.0
~ $ node
node    nodejs  
~ $ nodejs --version
v6.8.1
~ $ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

A combination of the few responses I've seen here. (Tested in FF, Chrome, IE11)

function functionName() 
{
   var myName = functionName.caller.toString();
   myName = myName.substr('function '.length);
   myName = myName.substr(0, myName.indexOf('('));
   return myName;
}

function randomFunction(){
    var proof = "This proves that I found the name '" + functionName() + "'";
    alert(proof);
}

Calling randomFunction() will alert a string that contains the function name.

JS Fiddle Demo: http://jsfiddle.net/mjgqfhbe/


Here is a one liner:

    arguments.callee.toString().split('\n')[0].substr('function '.length).replace(/\(.*/, "").replace('\r', '')

Like this:

    function logChanges() {
      let whoami = arguments.callee.toString().split('\n')[0].substr('function '.length).replace(/\(.*/, "").replace('\r', '');
      console.log(whoami + ': just getting started.');
    }

(function f() {
    console.log(f.name);  //logs f
})();

Typescript variation:

function f1() {} 
function f2(f:Function) {
   console.log(f.name);
}

f2(f1);  //Logs f1

Note only available in ES6/ES2015 compliant engines. For more see


This a variant of Igor Ostroumov's answer:

If you would like to use it as a default value for a parameter, you need to consider a second level call to 'caller':

function getFunctionsNameThatCalledThisFunction()
{
  return getFunctionsNameThatCalledThisFunction.caller.caller.name;
}

This would dynamically allow for a reusable implementation in multiple functions.

function getFunctionsNameThatCalledThisFunction()
{
  return getFunctionsNameThatCalledThisFunction.caller.caller.name;
}

function bar(myFunctionName = getFunctionsNameThatCalledThisFunction())
{ 
  alert(myFunctionName);
}

// pops-up "foo"
function foo()
{
  bar();
}

function crow()
{
  bar();
}

foo();
crow();

If you want the file name too, here is that solution using the answer from F-3000 on another question:

function getCurrentFileName()
{
  let currentFilePath = document.scripts[document.scripts.length-1].src 
  let fileName = currentFilePath.split('/').pop() // formatted to the OP's preference

  return fileName 
}

function bar(fileName = getCurrentFileName(),  myFunctionName = getFunctionsNameThatCalledThisFunction())
{
  alert(fileName + ' : ' + myFunctionName);
}

// or even better: "myfile.js : foo"
function foo()
{
  bar();
}

Try:

alert(arguments.callee.toString());

The answer is short: alert(arguments.callee.name);

참고URL : https://stackoverflow.com/questions/1013239/can-i-get-the-name-of-the-currently-running-function-in-javascript

반응형