Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I know i'm most probably nitpicking but... i have the following code:

var book;
var i=0;

i = 300;    
book = new CBook();
book.Title = "blahblah";
book.Contents = "lalalala";


function CBook() {
    this.Title = "";
    this.Contents = "";
}

now my question is:

would it be better to have

var book = {};

instead of

var book;

in the first version typeof(book) returns undefined before the assignment book = new CBook();

thanks in advance

share|improve this question
up vote 12 down vote accepted

would it be better to have

var book = {};

No, for a couple of reasons:

  1. You'd be creating an object just to throw it away when you did book = new Book(...) later. (Of course, a really good JavaScript engine might realize that and optimize it out. But still...)

  2. If you use lint tools, you'd be actively preventing them from warning you that you attempt to use book before you (properly) initialize it.

  3. If for some reason you had logic in your code that needed to check whether you'd assigned a book yet, you'd lose the ability to do that. By leaving the variable with its default undefined value, that check can be if (!book) { /* Not assigned yet */ }. (Of course, naive lint tools may warn you about that, too.)

#2 goes for the = 0 in var i = 0; as well.

But if you feel strongly about initializing book at the declaration, null would probably be a better choice.

share|improve this answer
1  
... and also loose a simple way to check, if the book object exists before it's created. – Teemu Jul 13 '13 at 18:13
    
@Teemu: Very good point, I've added it. – T.J. Crowder Jul 13 '13 at 18:15

A variable should be declared closest to its first use. So, if I were you, I would change to:

var book = new CBook();

Also, I would pass parameters to CBook and use it as a constructor:

var book = new CBook("blahblah", "lalalala");

function CBook(title, contents) {
   this.title = title;
   this.contents = contents;
}
share|improve this answer
1  
Not necessary at all, all variable declarations are hoisted. Hence if you do this in the middle of the code, var book; is executed at the beginning of the code anayway. – Teemu Jul 13 '13 at 18:07
1  
Hoisting happens within JS engine, the recommendation is for code readability. – Wand Maker Jul 13 '13 at 18:08
3  
i like declaring all variables in the beginning of every function – MIrrorMirror Jul 13 '13 at 18:08
1  
@WandMaker I think recommendations depend on the recommendator ; ). – Teemu Jul 13 '13 at 18:10
2  
@WandMaker: Indeed. Such as putting all variable declarations at the beginning of the scope in which they're used. See? Your "readable" and my "readable" don't match. :-) Of course, in practice, functions should be so short that "at the beginning of the function" is "close to first use" and the issue goes away. (Along with a lot of other ones.) – T.J. Crowder Jul 13 '13 at 18:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.