What is babel-plugin-transform-function-bind?
The babel-plugin-transform-function-bind package is a Babel plugin that allows you to use the bind operator (::) in your JavaScript code. This operator provides a shorthand for binding functions to a specific context, making your code more concise and readable.
What are babel-plugin-transform-function-bind's main functionalities?
Function Binding
This feature allows you to bind a function to a specific context using the :: operator. In the example, the greet function is bound to the obj context, ensuring that this.name refers to 'Alice' when the function is called.
const obj = {
name: 'Alice',
greet() {
console.log(`Hello, ${this.name}!`);
}
};
const greet = obj::obj.greet;
greet(); // Output: Hello, Alice!
Method Extraction
This feature allows you to extract a method from an object and bind it to a different context. In the example, the greet method is extracted from obj and then called with a different context, resulting in 'Hello, Charlie!'.
const obj = {
name: 'Bob',
greet() {
console.log(`Hello, ${this.name}!`);
}
};
const greet = ::obj.greet;
greet.call({ name: 'Charlie' }); // Output: Hello, Charlie!
Other packages similar to babel-plugin-transform-function-bind
babel-plugin-transform-class-properties
This package allows you to use class properties in your JavaScript code. While it doesn't provide the same function binding capabilities as babel-plugin-transform-function-bind, it does offer a way to define methods and properties directly within a class, which can sometimes be used to achieve similar results.
babel-plugin-transform-function-bind
Compile the new function bind operator ::
to ES5.
Detail
obj::func
func.bind(obj)
obj::func(val)
func.call(obj, val)
::obj.func(val)
func.call(obj, val)
Try in REPL
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));
Try in REPL
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);
Try in REPL
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-transform-function-bind
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["transform-function-bind"]
}
Via CLI
babel --plugins transform-function-bind script.js
Via Node API
require("babel-core").transform("code", {
plugins: ["transform-function-bind"]
});
References