Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the global variables used in my javascript [duplicate]

Tags:

javascript

Possible Duplicate:
Fetching all (javascript) global variables in a page

My application is using global variables in javascript. Is there a way to find how many of them are there?

Thanks Om

like image 597
Ohm Avatar asked Dec 28 '12 00:12

Ohm


People also ask

What are global variables in JavaScript?

Global Variables in JavaScript Explained. Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using var for use only within that function’s scope. If you declare a variable without using var, even if it’s inside a function, it will still be seen as global:

How do you declare a global variable inside a function?

Declaring JavaScript global variable within function To declare JavaScript global variables inside function, you need to use window object. For example: window.value=90; Now it can be declared inside any function and can be accessed from any function.

Why do we assign the global variable to a window?

Alternatively, assign the property to a window because, in browsers, global variables declared with var are properties of the window object: In ECMAScript 2015 specification, let, class, and const statements at global scope create globals that are not properties of the global object.

What is the difference between global and local variables in Python?

Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using var for use only within that function’s scope. If you declare a variable without using var, even if it’s inside a function, it will still be seen as global:


2 Answers

I made one.

var GlobalTester = (function(){
    var fields = {};
    var before = function(w){
        for(var field in w){
            fields[field] = true;
        };
    };

    var after = function(w){
        for(var field in w){
            if(!fields[field]){
                 console.log(field + " has been added");
            }            
        };

    };
    return {before: before, after:after};
}());

GlobalTester.before(window);

// Run your code here        
window.blar = "sdfg";      

GlobalTester.after(window);        
​
​

This will output blar has been added in the console

like image 187
david Avatar answered Oct 12 '22 22:10

david


Try this in your browser developer window (F12):

Object.keys(window).length
like image 25
Mitch Denny Avatar answered Oct 12 '22 23:10

Mitch Denny