Skip to content

Playground Proposal: Mallet operator#516

Merged
sebmck merged 2 commits intobabel:masterfrom
jridgewell:mallot
Jan 17, 2015
Merged

Playground Proposal: Mallet operator#516
sebmck merged 2 commits intobabel:masterfrom
jridgewell:mallot

Conversation

@jridgewell
Copy link
Copy Markdown
Member

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.

@jridgewell jridgewell changed the title Playground Proposal: Mallot operator Playground Proposal: Mallet operator Jan 16, 2015
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
@sebmck sebmck merged commit ecd85f5 into babel:master Jan 17, 2015
@sebmck
Copy link
Copy Markdown
Contributor

sebmck commented Jan 17, 2015

Thanks! I'll have to refactor this a bit as there's a lot of duplicate code shared between this and the memoization operator.

@sebmck
Copy link
Copy Markdown
Contributor

sebmck commented Jan 17, 2015

@jridgewell Can you also send a pull request that adds this to the docs? Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break because obj is going to be evaluated multiple times.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😞 Damn, hadn't considered that.

@sebmck
Copy link
Copy Markdown
Contributor

sebmck commented Jan 18, 2015

Released as of 2.13.0.

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
JacopKane pushed a commit to JacopKane/babel that referenced this pull request Jan 11, 2018
jridgewell added a commit to jridgewell/babel that referenced this pull request Feb 16, 2018
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
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 8, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants