Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
babel-plugin-transform-function-bind
Advanced tools
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.
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!
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.
Compile the new function bind operator
::
to ES5.
obj::func
// is equivalent to:
func.bind(obj)
obj::func(val)
// is equivalent to:
func.call(obj, val)
::obj.func(val)
// is equivalent to:
func.call(obj, val)
const box = {
weight: 2,
getWeight() { return this.weight; },
};
const { getWeight } = box;
console.log(box.getWeight()); // prints '2'
const bigBox = { weight: 10 };
console.log(bigBox::getWeight()); // prints '10'
// Can be chained:
function add(val) { return this + val; }
console.log(bigBox::getWeight()::add(5)); // prints '15'
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);
When nothing is specified before the ::
operator, the function is bound to its object:
$('.some-link').on('click', ::view.reset);
// is equivalent to:
$('.some-link').on('click', view.reset.bind(view));
npm install --save-dev babel-plugin-transform-function-bind
.babelrc
(Recommended).babelrc
{
"plugins": ["transform-function-bind"]
}
babel --plugins transform-function-bind script.js
require("babel-core").transform("code", {
plugins: ["transform-function-bind"]
});
FAQs
Compile function bind operator to ES5
We found that babel-plugin-transform-function-bind demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.