2

I converted my CoffeeScript code to JavaScript using http://js2coffee.org

ResetControls = ->
  $("#menu_item_url").val ""
  $("#menu_item_name").val ""
  $("#resource_id").prop "selectedIndex", 0
  $("#resource_type_id").prop "selectedIndex", 0

It converted it to:

var ResetControls;

ResetControls = function() {
  $("#menu_item_url").val("");
  $("#menu_item_name").val("");
  $("#resource_id").prop("selectedIndex", 0);
  return $("#resource_type_id").prop("selectedIndex", 0);
};

The first line in the converted JavaScript code suggests that its some kind of a best practice to put var keyword before variable name, when assigning function to a variable. Is it so? How?

In my understanding, var comes handy in recursive calls where your intention is to make copies of variables inside the recursive function (otherwise they will be shared or remain static between the recursive calls ).

Is there any other significance of var?

Annie
  • 3,090
  • 9
  • 36
  • 74
  • When a machine does a translation from one language to another, you really can't take what it outputs as "best practice". It *might* be, or it might just be that it takes into account some special situation that this code doesn't touch. – JJJ May 29 '13 at 11:28
  • http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript – Harsha Venkataramu May 29 '13 at 11:28
  • If we write var before variable inside function, it will be local variable. If we don't write var inside variable, it will be global. Normally, global vaiable are not preferred. So, var is used in javascript. I don't know about js2coffee – web2students.com May 29 '13 at 11:29
  • That's why you need to learn js **before** starting using coffeescript – zerkms May 29 '13 at 11:29
  • zerkms :P nice comment but I think I know js. I don't say I am expert but I am intermediate in js( in my views). and the thing i said global and local is correct (may be in bad language). – web2students.com May 29 '13 at 11:36
  • 1
    @nnnnnn, actually asking both. :) – Annie May 29 '13 at 11:48
  • 1
    @zerkms, and I thought that my argument about recursive function would imply that I am not n00b in JS world. But apparently not! So yeah, Yes I am familier with JS and comfortable with Coffee too! This conversion happened to be a part of cross-project DRY exercise. – Annie May 29 '13 at 11:52

2 Answers2

5

Yes, you have to put var before variable name. In this way you are declaring variable in the current scope - otherwise JS will search for it in outer scopes and if not declared in any scope - will make it global. As far as I know, in strict mode, even global variables should be declared with var in the global space.

P.S. This is valid for all variables, not only for those that you assign a function to

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
1

And another reason is garbage collection. Any variable created without the var keyword is created at the global scope and is never eligible for garbage collection, presenting the opportunity for a memory leak.

Manikandan
  • 3,025
  • 2
  • 19
  • 28