Skip to main content

@babel/plugin-transform-proto-to-assign

Detail

This means that the following will work:

JavaScript
var foo ={a:1};
var bar ={b:2};
bar.__proto__= foo;
bar.a;// 1
bar.b;// 2

however the following will not:

JavaScript
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.

Example

In

JavaScript
bar.__proto__= foo;

Out

JavaScript
function_defaults(obj, defaults){...}

_defaults(bar, foo);

Installation

  • npm
  • Yarn
  • pnpm
  • Bun
npm install --save-dev @babel/plugin-transform-proto-to-assign

Usage

babel.config.json
{
"plugins":["@babel/plugin-transform-proto-to-assign"]
}

Via CLI

Shell
babel --plugins @babel/plugin-transform-proto-to-assign script.js

Via Node API

JavaScript
require("@babel/core").transformSync("code",{
plugins:["@babel/plugin-transform-proto-to-assign"],
});

References

AltStyle によって変換されたページ (->オリジナル) /