Skip to content Skip to sidebar Skip to footer

What Does This Javascript Code Mean?

Possible Duplicate: What does “options = options || {}” mean in Javascript? Looking at the YouTube source... var yt = yt || {}; Does that mean.. set yt to yt if yt exists,

Solution 1:

Assign the value of yt back to yt unless it is any of 0, NaN, false, null, "", or undefined (i.e. it's falsy), in which case assign {} to yt.

This works because each of the values in the list above evaluate to false in a boolean expression.

Solution 2:

It means exactly that: If the content does not evaluate to false, assign it to itself (which is a neutral operation), otherwise create a new object and assign it to yt. It's typically used to instantiate objects to use as namespaces, first checking if the namespace already exists.

Solution 3:

Evaluate yt, if it evaluates falsey, then instantiate it as an object.

The first time I saw somthing like this was :

functionhandleEvent(e){
    e=e||window.event;
}

pretty nifty~ anyone know of other languages that support this syntax? (Not PHP =(

Solution 4:

Yes, the whole right side of the expression is evaluated first before the assignment. So if yt==false the value of the expression on the RHS will be {} and get passed to the var yt

Post a Comment for "What Does This Javascript Code Mean?"