-1

I prefer do function declaration

doSomething() //still works
function doSomething() {}

over

var doSomething = function() = {}
doSomething()

because with the function declaration, I don't have to worry about the order, it just got hoisted at the top. Now when it come to es6, my coworker like to do const doSomething = () => {} because for them they dislike the word 'function'. I lost the hoisting how can I fix it?

I hope I can do this

abc()
abc() => {}

but I have to use babel so that the word function can be ignore to make a function in es6/es7?

Casa Lim
  • 355
  • 3
  • 5
  • 15
  • If you are targeting a browser or environtment that supports ES6, you won't need `babel`, so it depends. – Oscar Paz Apr 17 '18 at 08:12
  • Arrow functions are not hoisted. And it's actually a good behaviour since it leas to more debuggable code. – Lewis Apr 17 '18 at 08:16
  • Tell your coworker that you dislike `const`, or (arrow) function expressions. Make your coworker understand that `function` declarations are not "deprecated" in any sense in ES6+. Establish a coding convention together. – Bergi Apr 17 '18 at 08:16

1 Answers1

1

No you can't:

abc();
abc() => {console.log('test');}

Moreover, arrow functions do now have their own this context and cannot be used as constructors. Which means they are not only for people who do not want to use the function keyword.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

klugjo
  • 19,422
  • 8
  • 57
  • 75
  • but we don't contructor that often, most of the time we just want to create function to 'store' business logic into smaller piece. I'm aware `this` is gone in arrow function, we find arrow function useful coz we write shorter code especially we don't have to explicitly return. – Casa Lim Apr 17 '18 at 08:16
  • Then just use arrow functions. It is good practice in my opinion to declare your functions before using them anyways. So do not rely on hoisting. – klugjo Apr 17 '18 at 08:17
  • ok make sense, there's no right or wrong thing, it's just preferences. – Casa Lim Apr 17 '18 at 08:21