Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify "Use searchable names" example #121

Merged
merged 2 commits into from
Jan 14, 2017
Merged

Simplify "Use searchable names" example #121

merged 2 commits into from
Jan 14, 2017

Conversation

mitogh
Copy link
Contributor

@mitogh mitogh commented Jan 12, 2017

There is no need to create an arrow function to execute a function inside of the timeout.

There is no need to create an arrow function to execute a function inside of the timeout.
@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jan 12, 2017

It is not always true. See, for example, this explanation.

Compare:

const launcher = {
  bang: 'bang!',

  blastOff() {
    console.log(this.bang);
  },

  timer() {
    setTimeout(() => {
      this.blastOff();
    }, 100);
  },
};

launcher.timer();
bang!
const launcher = {
  bang: 'bang!',

  blastOff() {
    console.log(this.bang);
  },

  timer() {
    setTimeout(this.blastOff, 100);
  },
};

launcher.timer();
undefined
const launcher = {
  bang: 'bang!',

  blastOff() {
    console.log(this.bang);
  },

  timer() {
    setTimeout(this.blastOff.bind(this), 100);
  },
};

launcher.timer();
bang!

I don't know, though, what is better for readability — the first code or the third one. But the second code could be an erroneous replacement if the function uses a call context, i.e. if it is not a static method.

@mitogh
Copy link
Contributor Author

mitogh commented Jan 12, 2017

I think that is most the way you define the callback function since setTimeout by definition expects a function as parameter the current example uses an anonymous function to just execute this function without looking into the way you use it.

Since the example does not assume any specific scenario or in what context is used.

@vsemozhetbyt
Copy link
Contributor

@mitogh Maybe then it would be better not to use this at all? this do assume some specific scenario or context.

@mitogh
Copy link
Contributor Author

mitogh commented Jan 12, 2017

I think that might be the best scenario I guess @vsemozhetbyt

@ryanmcdermott ryanmcdermott merged commit 84f8ea6 into ryanmcdermott:master Jan 14, 2017
@ryanmcdermott
Copy link
Owner

Thanks Crisoforo!

@mitogh
Copy link
Contributor Author

mitogh commented Jan 14, 2017

No problem man thank you!

@mitogh mitogh deleted the simplify-example branch January 14, 2017 23:09
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.

3 participants