-1

Does anyone know what this code does? It is written in Javascript.

var RocknCoder = RocknCoder || {}; 
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Ken Shoufer
  • 1,393
  • 4
  • 10
  • 14

1 Answers1

2

Short for:

if (!RocknCoder) var RocknCoder = {};

{} is an object initializer. Try verbalizing it and it makes more sense:

set variable RocknCoder equal to RocknCoder or {}

I believe that using the short form is better than the if simply because it blocks a ReferenceError from occurring on the if condition if the variable is not already declared in scope. var on the declaration may obviate that with hoisting, though.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • Interestingly, because of scope, this line of code does not work if the first declaration was outside a function and the second was inside. `(function () {var x = x || "boo"; return x;}()) === "boo";` even if `x` was already defined. – Brian Nickel Aug 23 '12 at 23:12
  • http://jsfiddle.net/bnickel/YAPW2/ When placed in a closure, the hoisting of the variable means that the variable on the righthand side is always `undefined`. The specific line of code in the question only works as desired when window-scoped. Doesn't affect the validity of your answer but a much more reliable approach would be `var RocknCoder = window.RocknCoder || {};` – Brian Nickel Aug 23 '12 at 23:24
  • The variable on the right-hand side is never called. In Firebug, I get `window, scope, window`. That observation is, strictly, not right. – Jared Farrish Aug 23 '12 at 23:28
  • Try this: http://jsfiddle.net/userdude/YAPW2/2/ The last statement has a local (closure) variable that sets itself (and not the `window.foo` variable). – Jared Farrish Aug 23 '12 at 23:31
  • That does work, but if you're doing two var definitions in one file you're doing something wrong. :) Whoever wrote the original code made the somewhat dangerous assumption that no one would ever try it in a closure. I personally would do `var x = window.x = window.x || {};` if I were to do such a thing. – Brian Nickel Aug 23 '12 at 23:50
  • Thanks for the detailed explanation Jared. I'm just starting to learn Javascript and jQuery Mobile. The concept of closure is still very vague to me. The full code is located at https://github.com/Rockncoder/JQMCalculator. – Ken Shoufer Aug 23 '12 at 23:52
  • @BrianNickel - You're making assumptions based on too little to make those types of assumptions. It's a straight-forward example. See: http://jsfiddle.net/userdude/YAPW2/3/ There's a part of scope that you're not quite getting. – Jared Farrish Aug 23 '12 at 23:52
  • @BrianNickel - And some would say if you're using global `var`s, you're doing something wrong. `;)` Double-declaring a `var` is a waste of bytes, whereas global variables are, at times, potentially "hazardous". – Jared Farrish Aug 23 '12 at 23:58
  • @KenShoufer - The code [here](https://github.com/Rockncoder/JQMCalculator/blob/master/scripts/app.js), I believe, keeps the variable that may have been predeclared in that or a higher scope, and then (apparently) sets those object properties and functions. Mostly it has to do if `RocknCoder` has already, for whatever reason, been declared before hand, it doesn't "undo" that. – Jared Farrish Aug 24 '12 at 00:01
  • @KenShoufer - See [Alnitak's answer here](http://stackoverflow.com/a/6439636/451969) for a good reason to do it this way. – Jared Farrish Aug 24 '12 at 00:03
  • @BrianNickel - See my comment about Alnitak's answer. – Jared Farrish Aug 24 '12 at 00:04
  • Fair points. Your /3 example shows that you really need to know where the other potential declaration of `RocknCoder` would be and your current scope. I just assumed it would be at the `window` level and is serving as a namespace. The `var` technically isn't wasteful if you're doing it in a closure. – Brian Nickel Aug 24 '12 at 00:05