Justin Ridgewell 616640a128 Playground Proposal: Mallet operator
The mallet operator is similar to the current memoization operator,
except it can be used outside of just objects.

In Ruby, it’s almost the same as `a = a || b`. Note that only `nil` and
`false` are falsey in Ruby. I’ve defined it as `== null`, though that
could be changed to any JS falsey value.
2015-01-16 18:57:15 -05:00

57 lines
797 B
JavaScript

var obj = {};
obj.x ||= 2;
assert.equal(obj.x, 2);
obj = {};
assert.equal(obj.x ||= 2, 2);
obj = { x: 1 };
obj.x ||= 2;
assert.equal(obj.x, 1);
obj = { x: 1 };
assert.equal(obj.x ||= 2, 1);
obj = { x: undefined }
obj.x ||= 2;
assert.equal(obj.x, 2);
obj = { x: undefined }
assert.equal(obj.x ||= 2, 2);
obj = { x: null }
obj.x ||= 2;
assert.equal(obj.x, 2);
obj = { x: null }
assert.equal(obj.x ||= 2, 2);
obj = { x: false }
obj.x ||= 2;
assert.equal(obj.x, false);
obj = { x: false }
assert.equal(obj.x ||= 2, false);
obj = undefined;
obj ||= 2;
assert.equal(obj, 2);
obj = undefined;
assert.equal(obj ||= 2 , 2);
obj = 1;
obj ||= 2;
assert.equal(obj, 1);
obj = 1;
assert.equal(obj ||= 2 , 1);
obj = null;
obj ||= 2;
assert.equal(obj, 2);
obj = null;
assert.equal(obj ||= 2 , 2);