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

在

JavaScript
bar.__proto__ = foo;

出

JavaScript
function _defaults(obj, defaults) { ... }

_defaults(bar, foo);

安装​

🌐 Installation

npm install --save-dev @babel/plugin-transform-proto-to-assign

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

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

通过 CLI​

🌐 Via CLI

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

通过 Node API​

🌐 Via Node API

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

参考​

🌐 References