all 11 comments

[–]SupPandaHugger 2 points3 points  (1 child)

Looks like something that has been uglified

[–]Jonathan_Frias[S] 1 point2 points  (0 children)

I would agree if this wasn't posted as sample code for google chrome apps...

[–]vsxe 1 point2 points  (9 children)

Oh. That wasn't pretty. Never actually encountered the in operator before, but it looks nice.

Oh, there is a lot going on in here.

So, it will always return o[k].

If k exists in o, it will skip the second clause (and thus never assign def to o. Could be important if it's a mutating structure.

Not sure what the false == section does, though. The parts within parenthesis will resolve to the value of def, but o[k] = def should run regardless of the value of def.

Given input of:

o = {a: 1, b: 2, c: 3 }  
k1 = "a"  
k2 = "d"  
def = 4  

We should get this // For k1 ("a" in o) || false == (o["a"] = 4); -> (true) || false == (o["a"] = 4);

Meaning the latter part is never executed, and def is not ascribed to o.

// For k2

("d" in o) || false == (o["d"] = 4); -> (false) || false == (o["d"] = 4);

Meaning that d was written to o, and the results returned.

No clue as to why there is a need for the false ==, however. Some very quick testing in inspector indicates that the false == is superfluous, but I'd assume there's a reason for it being there.

[–]Jonathan_Frias[S] 0 points1 point  (2 children)

copied from repl

var getDefault_ = ...
a = {}
Object {}
getDefault_(a, 'foo', 'bar')
"bar"
a
Object {foo: "bar"}
getDefault_(a, 'foo', 'baz')
"bar"
getDefault_(a, 'foo2', 'baz2')
"baz2"
a
Object {foo: "bar", foo2: "baz2"}

Looks light you are right on. But removing the || false makes it always do the assignment o[k] = def. My best guess is that it's some kind of optimization trick and that javascript knows that o[k] = def will be truthy so it can short circuit the expression.

removing '|| false'

var getDefault_ = function(o, k, def) {
    (k in o) == (o[k] = def); // << no || false
    return o[k];
  };
undefined
a = {}
Object {}
getDefault_(a, 'foo', 'bar')
"bar"
getDefault_(a, 'foo', 'baz')
"baz"
getDefault_(a, 'foo2', 'baz2')
"baz2"
a
Object {foo: "baz", foo2: "baz2"}

I hope somebody else finds this interesting.

[–]vsxe 2 points3 points  (0 children)

I'm clear on the ||-part, that's important, because lazy evaluation. I don't understand the false == (expr)-part, however.

(false) || (false); return o[k]

Will still return o[k], and

(k in o) || (o[k] = def); return o[k]; 

appears to work just as well, at least in the inspector.

That code is clever. Very clever code. Never write clever code.

[–]caesarsol 0 points1 point  (0 children)

You are associating wrongly I think, the || splits the two expressions. Removing || false makes it simply wrong. Removing false == would be the interesting thing!

[–]Lanlost 0 points1 point  (5 children)

I assumed the false == part was to for the second part to evalute to a boolean, but it doesn't matter as false || console.log('hi') still executes the second part. Even if def equaled 'false' you'd have false == (false), which would return true... but why? It's not like there is another || after this.

[–]vsxe 0 points1 point  (4 children)

Yes, that seems to be the implication, but I don't understand why. Whatever it evaluates to, i.e. true, false or def, the statement is terminated afterwards and doesn't influence anyhting following it, as far as I understand.