admin管理员组文章数量:1326328
In JavaScript, is there any benefit in assigning a temporary value to a newly declared variable? For example...
var a = 0,
b = '';
// Somewhere else
a = 5;
b = 'Goodbye';
vs
var a, b;
// Somewhere else
a = 5;
b = 'Goodbye';
I'm aware that assigning a variable on declaration will set its type. But in JavaScript, this can be easily changed by assigning a value of a different type, so doesn't really protect it in any way.
What are the advantages/disadvantages of the above?
In JavaScript, is there any benefit in assigning a temporary value to a newly declared variable? For example...
var a = 0,
b = '';
// Somewhere else
a = 5;
b = 'Goodbye';
vs
var a, b;
// Somewhere else
a = 5;
b = 'Goodbye';
I'm aware that assigning a variable on declaration will set its type. But in JavaScript, this can be easily changed by assigning a value of a different type, so doesn't really protect it in any way.
What are the advantages/disadvantages of the above?
Share Improve this question edited Mar 9, 2012 at 8:54 Stuart Memo asked Mar 8, 2012 at 14:33 Stuart MemoStuart Memo 1,1784 gold badges14 silver badges31 bronze badges 3- 1 Better variable names should help a lot in recognizing what the type should be. – pimvdb Commented Mar 8, 2012 at 14:37
- @pimvdb , he didn't related to the variables' names , just the fact of declaring them with values according to their type – Ofir Baruch Commented Mar 8, 2012 at 14:39
- 1 @Ofir Baruch: What I mean is that a good variable name should (implicitly or not) include the type in some way, so that setting them like this is not crucial to understanding the code. – pimvdb Commented Mar 8, 2012 at 14:41
5 Answers
Reset to default 4I believe that the advantage is: Order in your code. So you'll know right from the beginning of the code which type is which var.
The disadvantage is more code lines and size of the file.
Benefits of declaring variable type in JavaScript
You can't do that, so n/a.
is there any benefit in assigning a temporary value to a newly declared variable?
Advantages:
- It can avoid error conditions later if you fail to set it
Disadvantages:
- Spending time / code / bandwidth assigning a variable you will never use
- Not having an error condition show up in testing if you fail to set it later
No real advantages here. Nor would I say there's much of a disadvantage if you can handle it being undefined until you set it. Understanding how types work, particularly in regards to equality, what happens when you mix different types among the same operators and pass-by-reference vs. pass-by-value is more important than trying to put strict typing paradigms on a dynamic language.
Now, when declaring object literals, however, it can save you some hassle to declare all properties ahead of time.
For instance
myObject.with.way.too.much.hierarchy
If you're plan had been to just add those properties as you go, you'd need to check for the existence of every property in that chain in any scenario where you can't be sure they've all been defined because attempting to access a property of a non-existing object, even if just to check to see if it's undefined will throw an error.
So you'd have something obscene like this:
if(myObject && myObject.with && myObject.with.way ...//ew
Note: I personally tend to declare all my vars up front in a function but more as a self-documenting thing.
You have to set an initial value if you will be using any methods on the thing-
var s='',A=[];
for(var i=0;i<5;i++){
s+=i;
A.push(i);
}
Otherwise, except as a minimalist kind of documentation, it is unnecessary.
the code might brake or you may get a unexpected result when you use the undefined variable..
setting a value to a variable helps you when you use it accidently without checking if it has a known value or type
本文标签: Benefits of declaring variable type through assignment in JavaScriptStack Overflow
版权声明:本文标题:Benefits of declaring variable type through assignment in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742202741a2432261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论