Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript: function expression vs arrow function

LEGEND ,
Aug 29, 2018 Aug 29, 2018

Hello, all,

I've only just started reading about arrow functions in JavaScript.  Not sure if I want to start using them, or not, but wanted to get a feel for how most of you prefer to code, function expression or arrow function?

V/r,

^ _ ^

675
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2018 Aug 29, 2018

Im not using them yet because lm more familiar with the old way plus l think l can determine where the function is better, the arrow function seems to get lost within the rest of the code plus of course its not compatible if you need to support older  browsers.

Old habits die hard, especially once lve been using something which then becomes habital.

One thing which is good about them is when scoping is involved you dont have to do something like

Iet self = this;

self.name = self.value

You can use this

this.name = this.value

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 29, 2018 Aug 29, 2018

I'm not sure to quite understand what you do mean when scoping without binding ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2018 Aug 29, 2018

If l use this.blah within a set time out function for example or some other functions it usually doesnt work.

I have to asign 'this' to a variable

If l use the arrow function l can use 'this' without assigning it to a variable

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 29, 2018 Aug 29, 2018

yes, in fact we say the same, but differently... I was meaning without binding

as

funct A(){

this.blah

function B(){

this.blah

}.bind(this)

}

now is

A() => {

this.blah

B() => {

this.blah

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2018 Aug 29, 2018

I think the answers will depend very much on the age of the person answering, how long they have been coding in js, and how much they code in other languages.

For me I tend to use function expressions, mainly because that is how I code, (and learned to) in C# so it is less confusing when switching between languages.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2018 Aug 29, 2018
LATEST

pziecina  wrote

I think the answers will depend very much on the age of the person answering, how long they have been coding in js, and how much they code in other languages.

I think you're right - I'm an old fart so use old fart workflows......................................which work without all the fuss and confusion.....that's what I think anyway!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines