最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting scope of function caller - Stack Overflow

programmeradmin0浏览0评论

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)

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)

Share Improve this question edited Sep 16, 2011 at 12:14 Lightness Races in Orbit 385k77 gold badges664 silver badges1.1k bronze badges asked Sep 16, 2011 at 12:04 AlexAlex 5,7247 gold badges44 silver badges65 bronze badges 3
  • 4 Firebug. Breakpoint. Stack trace. That is all. – Lightness Races in Orbit Commented Sep 16, 2011 at 12:14
  • For some fun reason firebug wasn't interested in giving me a stack trace. – Alex Commented Sep 16, 2011 at 12:26
  • well, since it was breaking on the returned function, I assume thats what they call an "anonymous" function :) – Alex Commented Sep 16, 2011 at 12:48
Add a comment  | 

1 Answer 1

Reset to default 17

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
发布评论

评论列表(0)

  1. 暂无评论