B
B
BB 82018-06-09 19:14:24
JavaScript
BB 8, 2018-06-09 19:14:24

Higher order functions?

var createScream = function(logger) {
return function(message) {
logger(message.toUpperCase() + "!!!")
}
}
const scream = createScream(message => console.log(message))
scream('functions can be returned from other functions')

The result will be the same if you write:
var createScream = function(logger) {
return function(message) {
logger(message.toUpperCase() + "!!!")
}
}
const scream = createScream(console.log)
scream('functions can be returned from other functions')

Why write in the first case:
const scream = createScream(message => console.log(message))

If I may:
const scream = createScream(console.log)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-06-09
@rockon404

There is no need, since you, in the first case, call the wrapper first and pass an argument to it. The wrapper passes the argument to console.log.
In the second case, you are passing the argument directly to console.log.

P
profesor08, 2018-06-09
@profesor08

The difference is one extra function call. This is also a demo that shows passing a previously declared function and an anonymous function that is already doing something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question