开发者

Why does modernizer not use var for ret variable

I was studying the source for the modernizer library and I"m wondering why they don't use the keyword var f开发者_运维问答or the variable ret in the second line of code? They use it when declaring the variable version right above it, but not for ret. Can anyone explain why they wouldn't use the keyword var?


They do use the var keyword. They just use it once and let it apply to all the variables they define at the start of the program (which is a common pattern for JS programming).

var version = '2.0.6', // This is a comma, not a semi-colon.
Modernizr = {},

… and whoops, while that is the second line of code, it isn't the line you were talking about. The principle is the same there though.


ret is defined with a var at the top of the injectElementWithStyles() declaration:

// Inject element with style element and some CSS rules
injectElementWithStyles = function( rule, callback, nodes, testnames ) {

  var style, ret, node,
      div = document.createElement('div');
  // ...
  // ...
  // ...

  return !!ret;
}


I haven't analyzed the entire script, but in general you skip the "var" declaration when you want the variable to have a global scope.

More info: http://www.w3schools.com/js/js_variables.asp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜