[フレーム]
Last Updated: February 25, 2016
·
1.505K
· iondrimba

Declaring variables inline (a better way)

So lately I've seen some people complaining about declaring variables inline the way JSLINT imposes. People argue that is not good at all for debugging purposes and its hard comment stuff out and etc..

One thing I've been doing to overcome this problem, is to declare variables with a default value, and after that, I set the values I really want, overcoming the "you cant debug problem".

function Demo() {
 var personId = 0,
 personName = '',
 personAge = 0;

 // here I set the values I need
 personId = $(whatever-selector-id).val(); //convert this to number
 personName = $(whatever-selector-name).text();
 personAge = $(whatever-selector-age).val(); //convert this to number
}

Now I know the types I should expect of these variables, giving me a clearer way to read the code.

AltStyle によって変換されたページ (->オリジナル) /