Playground Proposal: Mallet operator#516
Merged
sebmck merged 2 commits intobabel:masterfrom Jan 17, 2015
jridgewell:mallot
Merged
Playground Proposal: Mallet operator#516sebmck merged 2 commits intobabel:masterfrom jridgewell:mallot
sebmck merged 2 commits intobabel:masterfrom
jridgewell:mallot
Conversation
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.
sebmck
added a commit
that referenced
this pull request
Jan 17, 2015
Playground Proposal: Mallet operator
Contributor
|
Thanks! I'll have to refactor this a bit as there's a lot of duplicate code shared between this and the memoization operator. |
Contributor
|
@jridgewell Can you also send a pull request that adds this to the docs? Thanks! |
Contributor
There was a problem hiding this comment.
This will break because obj is going to be evaluated multiple times.
Member
Author
There was a problem hiding this comment.
This is to avoid an unnecessary var declaration.
// obj.x ||= 2
if (!obj.x) obj.x = 2
// As opposed to
var _obj = obj;
if (!_obj.x) _obj.x = 2
Contributor
There was a problem hiding this comment.
What about setters defined on global? We can't evaluate nodes more than what's expected. It's the same reason that we have to work out the computed key etc.
Object.defineProperty(global, "obj", {
get: function () {
// this is going to get called every time you reference `obj` when it should only be evaluated a limited amount of times
}
});
obj ||= 2; // should only "get" obj once
Member
Author
There was a problem hiding this comment.
😞 Damn, hadn't considered that.
Contributor
|
Released as of 2.13.0. |
Closed
loganfsmyth
pushed a commit
to loganfsmyth/babylon
that referenced
this pull request
Mar 20, 2016
The mallet will check to see if the variable is falsey, and if it is, override it. It's almost the same as `a = a || b`. Re: babel/babel#516
jridgewell
added a commit
to jridgewell/babel
that referenced
this pull request
Feb 16, 2018
https://github.com/jridgewell/proposal-logical-assignment I'm bringing it [back](babel#516). 😉
jridgewell
added a commit
that referenced
this pull request
Feb 18, 2018
* Proposal: Logical Assignment Operators https://github.com/jridgewell/proposal-logical-assignment I'm bringing it [back](#516). 😉 * Use expectPlugin * Add to stage 0 preset * Add logicalAssignment missing plugin log stuff
aminmarashi
pushed a commit
to aminmarashi/babel
that referenced
this pull request
Mar 17, 2018
* Proposal: Logical Assignment Operators https://github.com/jridgewell/proposal-logical-assignment I'm bringing it [back](babel#516). 😉 * Use expectPlugin * Add to stage 0 preset * Add logicalAssignment missing plugin log stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 onlynilandfalseare falsey in Ruby. I’ve defined it as== null, though thatcould be changed to any JS falsey value.