Reputation: 48415
I am refactoring some javascript code from a previous project where the developer is no longer involved.
To keep short and to the point consider this simple code in a .js file
var a;
a = b;
These are the first 2 lines of the file. Basically it is just creating a variable of a
to reference the 'file global' (something defined in another js file) value of b
.
But is this not just the same as doing:
var a = b;
Is it safe to refactor this simple change, or are there some hidden dangers that are not obvious?
If it makes any difference, b
is just a simple object such as:
var b = { val1: '1', val2: '2' };
Upvotes: 1
Views: 53
Reputation: 10175
These two statements are identical. Thus a
is just an alias for the global b
.
Though we have to note that the arrangement of script references on the html page can cause a very popular problem. if the file that contains b
's declaration goes after the file that contains the lines:
var a;
a = b;
then the browser is going to throw an error.
NOTE: The second example is just lighter in terms of JavaScript file size. Thus reducing HTTP request load which is not pretty obvious.
Upvotes: 4