This means that the following will work:
var foo = { a: 1 };
var bar = { b: 2 };
bar.__proto__ = foo;
bar.a; // 1
bar.b; // 2
however the following will not:
var foo = { a: 1 };
var bar = { b: 2 };
bar.__proto__ = foo;
bar.a; // 1
foo.a = 2;
bar.a; // 1 - should be 2 but remember that nothing is bound and it's a straight copy
This is a case that you have to be aware of if you intend to use this plugin.
In
bar.__proto__ = foo;
Out
function _defaults(obj, defaults) { ... }
_defaults(bar, foo);
npm install --save-dev @babel/plugin-transform-proto-to-assign
yarn add --dev @babel/plugin-transform-proto-to-assign
pnpm add --save-dev @babel/plugin-transform-proto-to-assign
{
"plugins": ["@babel/plugin-transform-proto-to-assign"]
}
babel --plugins @babel/plugin-transform-proto-to-assign script.js
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-proto-to-assign"],
});
© 2014-present Sebastian McKenzie
Licensed under the MIT License.
https://babeljs.io/docs/babel-plugin-transform-proto-to-assign/