admin管理员组

文章数量:1221766

Is there a reason not to use arrows instead of regular function expressions in expressjs for handlers in middleware?

app.use(mountSomething())
router.use(mountSomethingElse())

app.get('/', (req,res,next)=> { 
    next();
})

route.get('/path', (req,res,next)=>{
    res.send('send')
})

Is there a reason not to use arrows instead of regular function expressions in expressjs for handlers in middleware?

app.use(mountSomething())
router.use(mountSomethingElse())

app.get('/', (req,res,next)=> { 
    next();
})

route.get('/path', (req,res,next)=>{
    res.send('send')
})
Share Improve this question edited Aug 3, 2016 at 6:54 Mulan 135k34 gold badges238 silver badges274 bronze badges asked Apr 18, 2016 at 4:08 G SisG Sis 1992 silver badges7 bronze badges 5
  • 2 What makes you think that you shouldn't use arrow functions instead of regular functions? – Saad Commented Apr 18, 2016 at 4:13
  • @saadq I've edited the question – G Sis Commented Apr 18, 2016 at 4:16
  • The only difference between a regular function expression and an arrow function is that the arrow function doesn't bind its own this value (You can read more about it here). So in a case like this where you won't need to use this, using an arrow function would be fine. – Saad Commented Apr 18, 2016 at 4:37
  • 1 If you not using generators inside handlers, you can totally use fat arrows. – Swaraj Giri Commented Apr 18, 2016 at 6:23
  • 1 No – Bergi Commented Apr 18, 2016 at 8:18
Add a comment  | 

1 Answer 1

Reset to default 18
app.get('/', (req,res,next)=> { 
    next();
})

is the same as

app.get('/', function(req,res,next) { 
        next();
}.bind(this))

In most cases you are not going to use 'this'(which will be probably undefined) in the handlers, so you are free to use arrow functions.

本文标签: javascriptes6 harmony arrow functions in express handlersStack Overflow