Aller au contenu principal

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

Traduction Bêta Non Officielle

Cette page a été traduite par PageTurner AI (bêta). Non approuvée officiellement par le projet. Vous avez trouvé une erreur ? Signaler un problème →

Détails

Cela signifie que les éléments suivants fonctionneront :

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

en revanche, les éléments suivants ne fonctionneront pas :

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

C'est un cas dont vous devez être conscient si vous prévoyez d'utiliser ce plugin.

Exemple

Entrée

JavaScript
bar.__proto__ = foo;

Sortie

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

_defaults(bar, foo);

Installation

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

Utilisation

Avec un fichier de configuration (Recommandé)

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

Via CLI

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

Via l'API Node

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

Références