Highlighted

JavaScript: function expression vs arrow function

LEGEND ,
Aug 29, 2018

Copy link to clipboard

Copied

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,

^ _ ^

Views

259

Likes

Translate

Translate

Report

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

JavaScript: function expression vs arrow function

LEGEND ,
Aug 29, 2018

Copy link to clipboard

Copied

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,

^ _ ^

Views

260

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2018 0
Adobe Community Professional ,
Aug 29, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2018 0
LEGEND ,
Aug 29, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2018 0
Adobe Community Professional ,
Aug 29, 2018

Copy link to clipboard

Copied

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

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2018 0
LEGEND ,
Aug 29, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2018 0
osgood_ LATEST
LEGEND ,
Aug 29, 2018

Copy link to clipboard

Copied

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!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2018 0