I’ll explain the this
part first because it’s not really that interesting
bind
's first argument controls what this
means inside the function. this
in javascript can refer to many things, depending on the context. bind
fixes what this
refers to.
this
is worth reading about, though it’s a bit confusing, don’t worry if you don’t get it right away.
Alrighty, partial application:
Partial application is used to create functions with some of the parameters “already filled in” - it’s like applying a function, but only applying some of the parameters, hence partially applying a function.
I’ll give a possibly bad example:
Consider making a function that takes a string and creates a new string that represents some valid html fragment:
makeH1(str) {
return `<h1>${str}</h1>`
}
That’s nice and all, but we can make a more general version:
makeHTMLElement(elementString, str){
return `<${elementString}>${str}</${elementString}>`;
}
With this we can make arbitrary elements more easily:
makeLi(str) {
return makeHTMLElement('li', str);
}
Partial application is essentially taking a function and putting default arguments in.
It comes from a more deeper idea in the sense that all functions of multiple arguments can be thought of as a chain of functions of a single argument.
As to where this is exactly useful, well, it’s hard to come up with concrete examples on the spot for me, but as you learn more functional programming you’ll see it come up a lot.
It’s fairly fundamental for a lot of the theory