What is @babel/plugin-proposal-function-bind?
@babel/plugin-proposal-function-bind is a Babel plugin that allows you to use the bind operator (::) in your JavaScript code. This operator is a syntactic sugar for function binding, making it easier to bind functions to a specific context.
What are @babel/plugin-proposal-function-bind's main functionalities?
Function Binding
This feature allows you to bind a function to a specific context using the bind operator (::). In this example, the greet function is bound to the obj context, so it correctly logs 'Hello, Alice!'.
const obj = {
name: 'Alice',
greet() {
console.log(`Hello, ${this.name}!`);
}
};
const greet = obj.greet::obj;
greet(); // Output: Hello, Alice!
Partial Application
This feature allows you to create partially applied functions using the bind operator (::). In this example, the add function is partially applied with the value 5, creating a new function add5 that adds 5 to its argument.
function add(a, b) {
return a + b;
}
const add5 = add.bind(null, 5);
console.log(add5(10)); // Output: 15
Other packages similar to @babel/plugin-proposal-function-bind
lodash
Lodash is a utility library that provides a wide range of functions for common programming tasks, including function binding. The _.bind method in Lodash can be used to bind functions to a specific context, similar to the bind operator in @babel/plugin-proposal-function-bind.
ramda
Ramda is a functional programming library for JavaScript that provides a variety of utility functions, including function binding. The R.bind method in Ramda can be used to bind functions to a specific context, offering similar functionality to the bind operator in @babel/plugin-proposal-function-bind.
@babel/plugin-proposal-function-bind
Compile the new function bind operator ::
to ES5.
Detail
obj::func
func.bind(obj)
::obj.func
obj.func.bind(obj)
obj::func(val)
func.call(obj, val)
::obj.func(val)
obj.func.call(obj, val)
Example
Basic
const box = {
weight: 2,
getWeight() { return this.weight; },
};
const { getWeight } = box;
console.log(box.getWeight());
const bigBox = { weight: 10 };
console.log(bigBox::getWeight());
function add(val) { return this + val; }
console.log(bigBox::getWeight()::add(5));
Using with document.querySelectorAll
It can be very handy when used with document.querySelectorAll
:
const { map, filter } = Array.prototype;
let sslUrls = document.querySelectorAll('a')
::map(node => node.href)
::filter(href => href.substring(0, 5) === 'https');
console.log(sslUrls);
document.querySelectorAll
returns a NodeList
element which is not a plain array, so you normally can't use the map
function on it, and have to use it this way: Array.prototype.map.call(document.querySelectorAll(...), node => { ... })
. The above code using the ::
will work because it is equivalent to:
const { map, filter } = Array.prototype;
let sslUrls = document.querySelectorAll('a');
sslUrls = map.call(sslUrls, node => node.href);
sslUrls = filter.call(sslUrls, href => href.substring(0, 5) === 'https');
console.log(sslUrls);
Auto self binding
When nothing is specified before the ::
operator, the function is bound to its object:
$('.some-link').on('click', ::view.reset);
$('.some-link').on('click', view.reset.bind(view));
Installation
npm install --save-dev @babel/plugin-proposal-function-bind
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/proposal-function-bind"]
}
Via CLI
babel --plugins @babel/proposal-function-bind script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/proposal-function-bind"]
});
References