Ramda JS Tutorial – Part 11 (bind, call, apply)

preview_player
Показать описание
Let's understand all the functions of the JavaScript library Ramda JS and learn a lot about functional programming as we go along.

💚 Support the channel on Patreon:

👾 In this video:
R.bind
R.call
R.apply

🖥 Full playlist:
Рекомендации по теме
Комментарии
Автор

A few things.
With the native bind() you can bind both context AND arguments.

For example, the following factory function:

const say = ( greeting, who ) => greeting + " " + who;
const helloFactory = () => say.bind( null, "hello" ); // dont care about context in this specific example
const sayHello = helloFactory();

console.log( sayHello( "world" ) ); // "hello world"

This is widely used instead of currying for Factory functions in JS.

As for JS classes, using function prototype is how people used to create JS classes before ECMA6. Now there is a class keyword, that is just syntactic sugar for what you are doing with the prototype. Do be aware however, that there are at least 3 types of inheritance in JS, and that prototypical inheritance ( the one you are doing ) is just one of them.

TheFlamePhoenix
Автор

The `this` context is set to the receiver of the function call. For example when you call `helloer.greet("john")` then `helloer` is the receiver and will be passed as `this`. When you pass the function as an argument, it is mostly called without receiver. `const call = (fn, val) => fn(val)` When `call` is called like `call(helloer.greet, "john")` then internally it is called as `fn("john")` which has no receiver, meaning that `this` is the global object or `undefined` when running strict mode.

Instead of binding the function you could also wrap it with an anonymous function, that calls it with receiver. Using the example above `call(name => helloer.greet(name), "john")`. Now when the anonymous function is called, it in turn calls `helloer.greet` with `helloer` as receiver and `"john"` as argument.

limintr
welcome to shbcf.ru