Get all Javascript Variables?

前端 未结 4 1621
感情败类
感情败类 2020-12-05 03:30

Is there a way for javascript to detect all assigned variables? For example, if one js file creates a bunch of vars (globally scoped), can a subsequent file get all the vars

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 04:00

    For Firefox, you can see the DOM tab -- easy, though not an answer to your question.

    The for in loop provided in Kinopiko's answer will work, but not in IE. More is explained in the article linked below.

    For IE, use the RuntimeObject.

    if(this.RuntimeObject){
        void function() {
            var ro = RuntimeObject(),
                results = [],
                prop;
            for(prop in ro) {
                results.push(prop);
            }
            alert("leaked:\n" + results.join("\n"));
        }();
    }
    

    See also:

    • Detecting Global Pollution with the JScript RuntimeObject (DHTML Kitchen article)
    • RuntimeObject (MSDN docs)

提交回复
热议问题