3

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, else create a new object?

If that's the case, I didn't think you can put a condition when declaring a variable.

Community
  • 1
  • 1
Marko
  • 71,361
  • 28
  • 124
  • 158
  • See [What does "options = options || {}" mean in Javascript?](http://stackoverflow.com/questions/2851404/what-does-options-options-mean-in-javascript) – Matthew Flaschen Feb 28 '11 at 00:55

4 Answers4

4

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.

Wayne
  • 59,728
  • 15
  • 131
  • 126
3

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.

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • Not exactly. Consider: `var yt = NaN; yt = yt || {};` In this case `yt` *exists*, but the final result will be `{}`. – Wayne Feb 28 '11 at 01:01
  • @lwburk : yes, but it's not an object. – Stefano Borini Feb 28 '11 at 01:02
  • 2
    That has nothing to do with it. The empty string is an object and will produce the same result. – Wayne Feb 28 '11 at 01:13
  • Hrm, perhaps I should revise. It's true that `"" instanceof Object` evaluates to `false`. I still think your wording is incorrect. If `yt` is the empty string or `0` (or whatever), then clearly it *exists*. – Wayne Feb 28 '11 at 02:20
1

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

The first time I saw somthing like this was :

function handleEvent(e){
    e=e||window.event;
}

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

Shad
  • 15,134
  • 2
  • 22
  • 34
  • Why would `yt` be false? It doesn't look like a boolean var. Or do you mean false as in the object doesn't yet exist ? – Marko Feb 28 '11 at 00:55
  • @marko false==undefined==null==0 in conditions in javascript – Chris Farmiloe Feb 28 '11 at 00:57
  • 1
    It's not whether `yt` is false, it's whether it's falsy. There are several things besides false that are [falsy](http://stackoverflow.com/questions/4535647/logical-operators-in-javascript-how-do-you-use-them/4535656#4535656), including (as Marko alluded to), `undefined`. – Matthew Flaschen Feb 28 '11 at 00:57
  • It's a matter of what is falsey~ http://stackoverflow.com/questions/3982663/falsey-values-in-javascript – Shad Feb 28 '11 at 00:57
  • Thanks @Matthew, I hadn't heard of `falsy` before. – Marko Feb 28 '11 at 00:59
  • Php actually does support this (assignment with an `||` or `or`), it just doesn't do what you think it would a whole lot of the time, so it's pretty useless other than for some error handling (`$result = mysql_query($query) or die(print_err(mysql_error())`). – jdd Feb 28 '11 at 02:09
  • @jeremiahd I use that syntax a lot but I had never thought of the parallel. =) (I wish it worked with assignment) – Shad Feb 28 '11 at 03:48
  • @Shad well it doesn't throw a syntax error. It does, however, mostly return the value the boolean operator returned instead of the value the lhs or rhs evaluated to. I'm pretty sure I've seen it act that way before, but I know that if it does, it's spotty :( . You can at least achieve the same thing with a ternary: `$yt = $yt ? $yt : Array();` – jdd Feb 28 '11 at 14:49
  • @jeremiahd Yeah, ternary assignment is fun :) – Shad Feb 28 '11 at 16:44
0

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

Chris Farmiloe
  • 13,935
  • 5
  • 48
  • 57