-7

The first way:

var a = 0;

setTimeout(DoSomething(a), 1000);

function DoSomething(a) {
    a = $(window).width();
}

The second way:

setTimeout(DoSomething, 1000);

function DoSomething() {
    var a = $(window).width();
}

What's way is better?

Feel
  • 51
  • 8
  • 5
    This isn't a matter of better or worse, these are doing different things. The scope is different. – brbcoding Mar 11 '14 at 20:07
  • 5
    The first one doesn't work at all. You're not calling the function when the timeout occurs, you're calling it immediately. – Barmar Mar 11 '14 at 20:07
  • Ignoring the other issues, it's often "better" to declare variables with the smallest scope possible to solve the task. – user2864740 Mar 11 '14 at 20:09
  • I believe you wanted the following for the first one: `setTimeout(function () { DoSomething(a); }, 1000);` – Hogan Mar 11 '14 at 20:09
  • possible duplicate of [What is the function of the var keyword in ECMAScript 262 3rd Edition/Javascript 1.5?](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-in-ecmascript-262-3rd-edition-javascript) – Michał Mar 11 '14 at 20:09

3 Answers3

1

Do you plan on using "a" again later on for reference?

Variables created inside of a function only exist within that function. Once that function is complete, it is destroyed.

function something(){
    var a = 1;
}

alert(a);
//This will trigger an alert box with nothing in it, or 'undefined'

Creating a variable outside of any function makes it a global variable. Global variables can be accessed and set by any other functions.

var a;
function something(){
    a = 10;
}

alert(a);
//This will trigger an alert box with '10' being displayed

So it really depends on whether or not you want to re-use the data being set in that variable again, or if it's a "one-time" use variable.

matcartmill
  • 1,573
  • 2
  • 19
  • 38
0

Javascript is functionally scoped, so in the first way, a is defined globally, while in the second, it's defined in the scope of the function DoSomething. Also note that in the first method, you're calling DoSomething incorrectly. Instead of calling it after the timeout, you're calling it immediately, and passing it's result (which is nothing in this example) as the function paramter input to setTimeout. As Hogan said in the comments, you'd want setTimeout(function () { DoSomething(a); }, 1000);

Generally, you should avoid defining variables at the global scope, and try to define them within a function. You can do this on document ready with jQuery:

$(function() {
    var a;
    var DoSomething = function() {
        a = $(window).width();
    }
    setTimeout(DoSomething, 1000);
};
xdumaine
  • 10,096
  • 6
  • 62
  • 103
0

Both are equally fine, what matters is the scope and context. If you need variable a to be accessible to more than just the function, the first example is fine. If you only want a to be available to the function and only that function the second approach is correct.

You need to understand variable scope to better understand the differences between the two cases.

Sean Quinn
  • 2,131
  • 1
  • 18
  • 48