For example:

function test( func ) { // можно ли здесь узнать контекст переданной ф-ии? } function whoami() { } const ctxA = { name: "A" }; const ctxB = { name: "B" }; test( whoami.bind( ctxA)); test( whoami.bind( ctxB)); test( whoami.bind( window)); 

  • 2
    No, it is impossible, it is accessible only from the inside - Grundy
  • @Grundy read in questions of 2010, which is impossible. I thought maybe the situation has changed, life is getting better? ES6, voodoo .. - Sergiks
  • one
    no, it seems to me it will never be possible to get, and there is no special meaning - Grundy
  • one
    The code execution context object is a “dump” in which the function parameters lie, the variable object (the temporary tree on which the function code works), the this property, the scope property. Every time an interpreter needs to take a variable, it goes to look at the context object CURRENT, if it is not there, it looks where the scope property refers to, goes to this stack object and “takes” the execution context below the stack object ( or the Closure object takes, if it comes across). The code execution context object is an ABSTRACT object (work model). - Dimon

2 answers 2

Is it possible to know the context of the transferred function?

No you can not.


With reference to the result of the bind function.

This function returns a not quite normal function , but a wrapper object. In the returned object, this function sets the [[BoundThis]] internal field - which is inaccessible from the code. And only when called, the value of this field is passed to the function and becomes available through the keyword this .

    The code execution context object is a “dump” in which the function parameters lie, the variable object (the temporary tree on which the function code works), the this property, the scope property. Every time an interpreter needs to take a variable, it goes to look at the context object CURRENT, if it is not there, it looks where the scope property refers to, goes to this stack object and “takes” the execution context below the stack object ( or the Closure object takes, if it comes across).

    The code execution context object is an ABSTRACT object (work model). If the code is global, then this code execution context object is called Global. If the code is a code inside a function, then such an object is the context of code execution called Local.

    This object cannot be "recognized", one can only use the variables and parameters that lie in it.

    The this property of the context function of executing the function code points to an object from the BOM, in which there is a property that refers to this function (points to the object in which this function lies).