Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting scope of function caller

I have a function that breaks somewhere in Line 1433 of ExtJS.

var createDelayed = function(h, o, scope){
console.log(arguments); //logs undefined all round. 
    return function(){
        var args = Array.prototype.slice.call(arguments, 0);
        setTimeout(function(){
            h.apply(scope, args);
        }, o.delay || 10);
    };
};

Is there any way to see what line a function is executed from, from within itself?

(since it's a third party lib, and I cant really do

var me =this;

and log me)

like image 311
Alex Avatar asked Sep 16 '11 12:09

Alex


People also ask

What is the scope of functions?

Such functions are called scope functions. There are five of them: let , run , with , apply , and also . Basically, these functions do the same: execute a block of code on an object. What's different is how this object becomes available inside the block and what is the result of the whole expression.

What does it mean to be function scoped?

Function scoped variables: A function scoped variable means that the variable defined within a function will not accessible from outside the function. Block scoped variables: A block scoped variable means that the variable defined within a block will not be accessible from outside the block.

Is scope and function the same?

The scope determines the accessibility of variables and other resources in the code, like functions and objects. JavaScript function scopes can have two different types, the locale and the global scope. Local variables are declared within a function and can only be accessed within the function.

What is function caller?

A Function Caller block calls and executes a function defined with a Simulink Function block or an exported Stateflow® function. Using Function Caller blocks, you can call a function from anywhere in a model or chart hierarchy. You can select a Simulink Function or Function Caller block to highlight related blocks.


1 Answers

There is arguments.callee.caller, which refers to the function that called the function in which you access that property. arguments.callee is the function itself.

There is no way to get the scope of the original function without passing it. In the following example, you cannot determine the this value inside foo (apart from knowing there is nothing special happening with this here):

function foo() {
    bar();
}

function bar() {
    console.log(arguments.callee);        // bar function
    console.log(arguments.callee.caller); // foo function
}

foo();

Documentation


To get the line number things becomes trickier, but you can throw an error and look at the stack trace: http://jsfiddle.net/pimvdb/6C47r/.

function foo() {
    bar();
}

function bar() {
    try { throw new Error; }
    catch(e) {
        console.log(e.stack);
    }
}

foo();

For the fiddle, it logs something similar to the following in Chrome, where the end of the line says the line number and character position:

Error
    at bar (http://fiddle.jshell.net/pimvdb/6C47r/show/:23:17)
    at foo (http://fiddle.jshell.net/pimvdb/6C47r/show/:19:5)
    at http://fiddle.jshell.net/pimvdb/6C47r/show/:29:1
like image 191
pimvdb Avatar answered Oct 21 '22 05:10

pimvdb