1

I've been using a new method that I just learned for keeping outside scope inside of another function but I can't remember the term for the syntax used. Below is an example of how it works:

Api.get().then( (data) => {
   console.log('do something with data', data);
}, ( e ) {
   console.log('Error: ', e );
});

Basically, its a callback function but what is the term or name of this type of syntax. The reason I like using this is because you can still reference this and its values and methods, whereas using function( data ) would lose this context. I want to know the name or term of this syntax because it doesn't seem to be supported by iphone or safari and I would like to look further into support and documentation on support for this method.

6
  • it is arrow function for the first one. the second one is a syntax error. you need (e) => {} Commented Jul 22, 2016 at 5:12
  • @torazaburo fat arrow function to be correct. Some call it incorrectly lambda Commented Jul 22, 2016 at 5:12
  • Because it's ES6, not all browsers support it yet. Commented Jul 22, 2016 at 5:13
  • Thanks @torazaburo I believe you are correct, Mozilla Developer Network refers to it as arrow functions developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. I knew it wasn't support just wanted to figure out what it was called so I could find out what browsers and versions were supporting it. Commented Jul 22, 2016 at 5:16
  • 1
    You can still use arrow functions to program while making your code compatible with older browsers, see my answer below for more on this :) Commented Jul 22, 2016 at 5:21

1 Answer 1

2

It's an arrow function and it's part of the latest 6th Edition of the ECMAScript standard (Also called ECMAScript 2015 since it was released in 2015). It's not as well supported by many browsers as you can see here.

You can use them in older browsers using something called a transpiler, which in essence all it does is convert ECMAScript 6 code (which isn't all that well supported) into more compatible ECMAScript 5 code. There are many, but Babel is the most popular one. You can try it out here and watch how arrow functions transform into compatible, ECMAScript 5 code that works across all browsers.

The reason I like using this is because you can still reference this and its values and methods

You can get the same results that arrow functions give you using bind(). Try this:

Api.get().then(function(data) {
   console.log('do something with data', data);
}.bind(this), function(e) {
   console.log('Error: ', e );
}.bind(this));

What bind() does is it creates a new function that sets whatever object you send to it as the first parameter, to this. This way you can still get access to the current scope you are working on. You can read more about bind() here.

I hope this answers your question.

Sign up to request clarification or add additional context in comments.

4 Comments

Well to be fair, in this case, the context isn't even used, so bind is unecessary. But you have the right idea.
I'm aware the context isn't used, but Tom specifically mentioned his main reason for using arrow functions is that he can use the this in the current lexical scope. This is why I suggested the use of bind().
Thank you for this, you answered my question in its entirety and within the context of what I was asking!!! I appreciate the clarification which was just what I was looking for!
function(data) => { looks not valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.