Name: proposal-logical-assignment
Owner: Ecma TC39
Description: A Stage 1 proposal to combine Logical Operators and Assignment Expressions
Created: 2018-02-16 04:00:35.0
Updated: 2018-05-22 19:54:50.0
Pushed: 2018-04-02 19:00:13.0
Size: 17
Language: null
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A Stage 1 proposal to combine Logical Operators and Assignment Expressions:
Or Or Equals" (or, the Mallet operator :wink:)
= b;
(a = b);
And And Equals"
= b;
(a = b);
ventually....
QQ Equals"
= b;
(a = b);
Convenience operators, inspired by Ruby's. We already have a dozen mathematical assignment operators, but we don't have ones for the often used logical operators.
tion example(a = b) {
Default assignment only works for `undefined`.
But I want any falsey to default
(!a) {
a = b;
tion numeric(a = b) {
Maybe I want to keep numeric 0, but default nullish
(a == null) {
a = b;
If statements work, but terseness would be nice.
tion example(a = b) {
Ok, but it triggers setter.
= a || b;
No setter, but sometimes flagged as a lint error!
|| (a = b);
With this, we get terseness and we don't have to suffer from setter calls.
The logical assignment operators function a bit differently than their mathematical assignment friends. While math assignment operators always trigger a set operation, logical assignment embraces their short-circuiting semantics to avoid it when possible.
x = 0;
t obj = {
t x() {
return x;
t x(value) {
console.log('setter called');
x = value;
his always logs "setter called"
x += 1;
rt.equal(obj.x, 1);
ogical operators do not call setters unnecessarily
his will not log.
x ||= 2;
rt.equal(obj.x, 1);
ut setters are called if the operator does not short circuit
setter called"
x &&= 3;
rt.equal(obj.x, 3);