Get the self function in JavaScript strict mode

I often need a function fto get the name of a method that points to f. For instance. Let's say we have a function getMethodName(obj, methodFunction)that uses foreachon objto find a property that is a reference to methodFunction:

obj = { 
    foo : function() {
        var myName = getMethodName(obj, arguments.callee); // "foo"
    }
}

How to do it in strict mode , where it is arguments.calleedeprecated?

+3
source share
2 answers

You cannot do this. And you should not do this in the first place, this is magic, your function should not depend on its caller, etc. Change your design so that it works again.

arguments.callee .caller - , , . , -, , , - .

: http://whereswalden.com/2010/09/08/new-es5-strict-mode-support-now-with-poison-pills/

+4

, , , , :

obj = { 
    foo : function METHODNAME() {
        var myName = getMethodName(obj, METHODNAME); // "foo"
    }
}

: , IE , METHODNAME ( ). , , , , , , eval, eval.

+1

Source: https://habr.com/ru/post/1784400/


All Articles