Defining a variable using a conditional operation in javascript

name = name || {}

generates a reference error while it var name = name || {}

works. How does javascript variable initialization work?

+3


source to share


4 answers


Line

var name = name || {};

      

is really interpreted like this:

// Upon entry to the scope
var name;

// Later, in step-by-step code
name = name || {}

      

Since it is name

declared, you can get its value (from name

the right side =

). Its value will be undefined

at this point, so it will name = name || {}

assign it {}

.

The big difference is here:



name = name || {}

      

... is that you have no declaration for name

, and therefore does not exist when the right-hand side of that assignment is evaluated. The horror of implicit globals allows you to create global variables implicitly by assigning to them, but prevents you from reading the value of a variable, which doesn't (yet).

So, version c var

works, and version doesn't throw ReferenceError

.


Side note. I recommend not relying on the horror of implicit globals. Instead, always specify your variables (and avoid global bindings if possible). You can use JavaScript "strict mode" to turn the horror of implicit globals into ReferenceError

(for example, writing to an unknown symbol becomes an error, not creating a global, just like reading).

+2


source


The second code works because of the rise of JavaScript.

If you have

var name = name || {}

      



It is ordered like

//moved to to top of scope
var name;

//point where you initialize it
name = name || {};

      

So, when you initialize the name, it is already declared and has a value undefined

. The first code did not receive the advertised name.

+2


source


name = name || {}

says "I already have a variable called name name

, set it to either name

or {}

. The problem is that you don't already have a variable called name. So when it skips to see what it should be set to , he thinks "aha, name

... oh wait, not not" and errors.

var name = ...

first creates a variable and then starts looking at what it should be, so when it gets an evaluation of the value, it says "aha, name

... I just did this, undefined This is false, so this {}

is."

In this situation, two other situations arise.

function a(name) { name = name || 'Joe'; }

      

where if is name

not passed (for example a()

) it will default to Joe. In this case, the variable is already defined by the function definition, so you don't need it var

.

defaults = { name: 'Joe' };
options = {};

// Later on
options.name = options.name || defaults.name;

      

0


source


When you use var

, javascript defines all the variables at the beginning of the scope. This is called swapping.

For example, if you have this code:

function doStuff() {
   for (var i = 0; i < arr.lenght; i++)
      var currentItem = arr[i]
      var doSomething = function(currentItem) { ... }
   }
}

      

javascript execute this code:

function doStuff() {
   var i,
       currentItem,
       doSomething;

   for (i = 0; i < arr.lenght; i++) {
      currentItem = arr[i]
      doSomething = function(currentItem) {...}
   }
}

      

0


source







All Articles