Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
babel-plugin-transform-es2015-arrow-functions
Advanced tools
Compile ES2015 arrow functions to ES5
The babel-plugin-transform-es2015-arrow-functions package is a Babel plugin that transforms ES2015 (ES6) arrow functions into ES5 function expressions. This is useful for ensuring compatibility with environments that do not support ES2015 syntax.
Transform Arrow Functions
This feature transforms ES2015 arrow functions into ES5 function expressions. This is particularly useful for ensuring that your code runs in environments that do not support ES2015 syntax.
const add = (a, b) => a + b;
// Transformed to:
var add = function(a, b) {
return a + b;
};
Lexical 'this' Binding
Arrow functions do not have their own 'this' context and instead inherit 'this' from the parent scope. This feature ensures that the 'this' context is correctly bound when transforming arrow functions to ES5.
const obj = {
value: 10,
increment: function() {
setTimeout(() => {
this.value++;
}, 1000);
}
};
// Transformed to:
var obj = {
value: 10,
increment: function() {
var _this = this;
setTimeout(function() {
_this.value++;
}, 1000);
}
};
This plugin transforms ES2015 function names to ensure compatibility with older environments. While it focuses on function names rather than arrow functions, it serves a similar purpose of making ES2015 features compatible with ES5.
This plugin transforms ES2015 parameter features like default parameters and rest parameters into ES5-compatible code. It complements babel-plugin-transform-es2015-arrow-functions by handling other ES2015 syntax features.
This plugin transforms ES2015 template literals into ES5 string concatenations. Like babel-plugin-transform-es2015-arrow-functions, it ensures that modern JavaScript syntax can run in older environments.
Compile ES2015 arrow functions to ES5
In
var a = () => {};
var a = (b) => b;
const double = [1,2,3].map((num) => num * 2);
console.log(double); // [2,4,6]
var bob = {
_name: "Bob",
_friends: ["Sally", "Tom"],
printFriends() {
this._friends.forEach(f =>
console.log(this._name + " knows " + f));
}
};
console.log(bob.printFriends());
Out
var a = function a() {};
var a = function a(b) {
return b;
};
var double = [1, 2, 3].map(function (num) {
return num * 2;
});
console.log(double); // [2,4,6]
var bob = {
_name: "Bob",
_friends: ["Sally", "Tom"],
printFriends: function printFriends() {
var _this = this;
this._friends.forEach(function (f) {
return console.log(_this._name + " knows " + f);
});
}
};
console.log(bob.printFriends());
npm install --save-dev babel-plugin-transform-es2015-arrow-functions
.babelrc
(Recommended).babelrc
// without options
{
"plugins": ["transform-es2015-arrow-functions"]
}
// with options
{
"plugins": [
["transform-es2015-arrow-functions", { "spec": true }]
]
}
babel --plugins transform-es2015-arrow-functions script.js
require("babel-core").transform("code", {
plugins: ["transform-es2015-arrow-functions"]
});
spec
- This option wraps the generated function in .bind(this)
and keeps uses of this
inside the function as-is, instead of using a renamed this
. It also adds a runtime check to ensure the functions are not instantiated.FAQs
Compile ES2015 arrow functions to ES5
The npm package babel-plugin-transform-es2015-arrow-functions receives a total of 1,336,983 weekly downloads. As such, babel-plugin-transform-es2015-arrow-functions popularity was classified as popular.
We found that babel-plugin-transform-es2015-arrow-functions 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.