Skip to content

Conversation

kikorb
Copy link
Contributor

@kikorb kikorb commented Mar 17, 2017

The example was wrong. It stated that ?= is similar to JS || when the || will affect not only not defined scenario but also any falsy values (values that evaluate as false)

There are way too many differences between the 2:

  1. window.var = false;
  2. window.var = 0;
  3. window.var = '';
  4. window.var = null;

window.var ?= 'something'
console.log(window.var) ->

  1. false
  2. 0
  3. ''
  4. 'something'

window.var ||= 'something'
console.log(window.var) ->

  1. 'something'
  2. 'something'
  3. 'something'
  4. 'something'

The example was wrong. It stated that ?= is similar to JS || when the || will affect not only not defined scenario but also any falsy values (values that evaluate as false)

There are way to many differences between the 2:

1. window.var = false;
2. window.var = 0;
3. window.var = '';
4. window.var = [];
5. window.var = null;

window.var ?= 'something'
console.log(window.var) ->
1. false
2. 0
3. ''
4. []
5. 'something'

window.var ||= 'something'
console.log(window.var) ->
1. 'something'
2. 'something'
3. 'something'
4. []
5. 'something'

{% highlight javascript %}
window.MY_NAMESPACE = window.MY_NAMESPACE || {};
if(window.MY_NAMESPACE == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since type coercion is the crux of the PR maybe this coerced comparison should also be expanded:

if (window.MY_NAMESPACE === null || window.MY_NAMESPACE === undefined) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you 👍

{% highlight javascript %}
window.MY_NAMESPACE = window.MY_NAMESPACE || {};
if(window.MY_NAMESPACE == null) {
window.MY_NAMESPACE = window.MY_NAMESPACE
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean window.MY_NAMESPACE = {};?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, sorry for noticing the typo, I did the changes quickly and that is never a good idea. 👍

@sukima sukima merged commit 685b09b into coffeescript-cookbook:master Mar 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants