From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../proxy/handler/construct/index.html | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 files/fr/web/javascript/reference/objets_globaux/proxy/handler/construct/index.html (limited to 'files/fr/web/javascript/reference/objets_globaux/proxy/handler/construct') diff --git a/files/fr/web/javascript/reference/objets_globaux/proxy/handler/construct/index.html b/files/fr/web/javascript/reference/objets_globaux/proxy/handler/construct/index.html new file mode 100644 index 0000000000..90eb5f0105 --- /dev/null +++ b/files/fr/web/javascript/reference/objets_globaux/proxy/handler/construct/index.html @@ -0,0 +1,137 @@ +--- +title: handler.construct() +slug: Web/JavaScript/Reference/Objets_globaux/Proxy/handler/construct +tags: + - ECMAScript 2015 + - JavaScript + - Méthode + - Proxy + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/construct +--- +
{{JSRef}}
+ +

La méthode handler.construct() est une trappe pour l'opérateur {{jsxref("Opérateurs/L_opérateur_new", "new")}}. Afin que l'opération new puisse être valide sur le proxy correspondant, la cible utilisée doit avoir une méthode interne [[Construct]] (autrement dit, l'instruction new cible doit être valide).

+ +
{{EmbedInteractiveExample("pages/js/proxyhandler-construct.html", "taller")}}
+ + + +

Syntaxe

+ +
var p = new Proxy(cible, {
+  construct: function(cible, listeArguments, newTarget) {
+  }
+});
+
+ +

Paramètres

+ +

Les paramètres suivants sont passés à la méthode constructthis est ici lié au gestionnaire (handler).

+ +
+
cible
+
L'objet cible.
+
listeArguments
+
La liste des arguments passés au constructeur.
+
newTarget
+
Le constructeur originellement appelé.
+
+ +

Valeur de retour

+ +

La méthode construct doit renvoyer un objet.

+ +

Description

+ +

La méthode handler.construct() est une trappe pour l'opérateur {{jsxref("Opérateurs/L_opérateur_new", "new")}}.

+ +

Interceptions

+ +

Ce trappe intercepte les opérations suivantes :

+ + + +

Invariants

+ +

Si les invariants suivants ne sont pas respectés, le proxy renverra une exception {{jsxref("TypeError")}} :

+ + + +

Exemples

+ +

Dans l'exemple qui suit, on piège l'opérateur {{jsxref("Opérateurs/L_opérateur_new", "new")}}.

+ +
var p = new Proxy(function() {}, {
+  construct: function(target, argumentsList) {
+    console.log("called: " + argumentsList.join(", "));
+    return { value: argumentsList[0] * 10 };
+  }
+});
+
+console.log(new p(1).value); // "appel sur : 1"
+                             // 10
+
+ +

Dans cette version, on ne respecte pas la contrainte d'invariance :

+ +
var p = new Proxy(function() {}, {
+  construct: function(target, argumentsList) {
+    return 1;
+  }
+});
+
+new p(); // Une exception TypeError est levée
+
+ +

Dans le code qui suit, le proxy n'est pas correctement initialisé. La cible du proxy doit être un constructeur valide qui puisse être utilisé avec new.

+ +
var p = new Proxy({}, {
+  construct: function(target, argumentsList, newTarget){
+    return {};
+  }
+});
+
+new p(); // TypeError: p is not a constructor
+ +

Spécifications

+ + + + + + + + + + + + + + + + + + + +
SpécificationÉtatCommentaires
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget', '[[Construct]]')}}{{Spec2('ES2015')}}Définition initiale.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget', '[[Construct]]')}}{{Spec2('ESDraft')}} 
+ +

Compatibilité des navigateurs

+ + + +

{{Compat("javascript.builtins.Proxy.handler.construct")}}

+ +

Voir aussi

+ + -- cgit v1.2.3-54-g00ecf