Determining Source Line and File of function reference - How does Firebug do it?

前端 未结 2 1406
清酒与你
清酒与你 2021-01-03 03:17

Brief introduction:

I\'m attempting to get at line number of function definition for parsing documentation comments on only public stuff. I\'ve gotten to the point

2条回答
  •  时光说笑
    2021-01-03 03:43

    Here's a potential solution that I haven't tested. A couple years ago, there was a security exploit that allowed JavaScript to redeclare constructors of native objects. John Walker gave this example:

    function Array() {
        this[1] = 50;
    }
    var a = [40];
    alert(a[0] + a[1]); // Gives 90
    

    In the same vein, perhaps it's possible to redeclare the function declaration in the browsers where the exploit exists?

    function Function() {
        // Should give the stack trace, complete with line number?
        alert(new Error().stack); 
    }
    
    window.x = function () {}
    

    I don't have the necessary browsers (John Resig cites Firefox 2, Opera 9, and Safari 3 as browsers where the Array exploit works), so I can't test it, but maybe this is a place to start?

提交回复
热议问题