JavaScript: function expression vs arrow function
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,
^ _ ^
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
Copy link to clipboard
Copied
I'm not sure to quite understand what you do mean when scoping without binding ?
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
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
}
}
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.
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!!

