As it is. Not. Since you do not use var to declare your variables, you declare these variables in the global scope, and not in the local scope.
If you are accessing the variable repeatedly, then it is definitely worth caching a copy in the local area. This is due to the fact that functions and objects have a chain of areas for searching if the variable is not available in the immediate area. In the following example, the innermost function must first search in its local region, then in the region of its parent function, and then finally in the outermost region of the function to find i . If i not present in this area, then he would also have to search for a global area.
(function () { var i = 10; (function () { (function() { console.log(i); // i is 10 })(); })(); })();
The problem with caching variables in the local area is that sometimes the code can become difficult to read, and to a large extent any readability of the code increases the trump card's efficiency (a computer handles inefficient code much better than a person with poorly written code).
Declare your variables at the top of your function
If you are really concerned about efficiency / correctness, then declare all your variables at the beginning of the function. This allows javascript to be very effective at creating a local scope of a function. Declarations inside the function are inefficient, since javascript must first check if there is free space in the local area and, if necessary, change the size of the area. Be warned, even if you do not declare your variables at the top of the function, some compilers will do this for you, as this is part of the ECMA specification, which may lead to some errors). a source
eg.
var i = 1; function test() { console.log(i); // undefined var i = 10; } test();
Essentially, the javascript engine compiled your code as:
var i = 1; function test() { var i; console.log(i); // undefined i = 10; } test();
The version of your code has been fixed (if you needed to perform further operations using a, b or c).
var obj = { a: 1, b: 2, c: 3, f: function() { var a, b, c; a = this.a; b = this.b; c = this.c; alert(a); alert(b); alert(c); } }