aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference
diff options
context:
space:
mode:
authorSphinxKnight <SphinxKnight@users.noreply.github.com>2021-12-19 07:01:23 +0100
committerGitHub <noreply@github.com>2021-12-19 07:01:23 +0100
commitb1e7241f9e66591988e7de54a514eb6c15d555eb (patch)
tree9d18ed09737a092575c1bf88c9ff5b3d58377ab0 /files/fr/web/javascript/reference
parenta0b7fb61a2618e6972789ae73a7c2c5e02dd52be (diff)
downloadtranslated-content-b1e7241f9e66591988e7de54a514eb6c15d555eb.tar.gz
translated-content-b1e7241f9e66591988e7de54a514eb6c15d555eb.tar.bz2
translated-content-b1e7241f9e66591988e7de54a514eb6c15d555eb.zip
Translation of untranslated pages in JS section (out of Global Objects subpages) (#3150)
* Translation of Class_static_initialization_blocks * Translation of Hash_outside_class * Translate working w private class features * Translate RegExp Cheatsheet
Diffstat (limited to 'files/fr/web/javascript/reference')
-rw-r--r--files/fr/web/javascript/reference/classes/class_static_initialization_blocks/index.md167
-rw-r--r--files/fr/web/javascript/reference/errors/hash_outside_class/index.md67
2 files changed, 234 insertions, 0 deletions
diff --git a/files/fr/web/javascript/reference/classes/class_static_initialization_blocks/index.md b/files/fr/web/javascript/reference/classes/class_static_initialization_blocks/index.md
new file mode 100644
index 0000000000..f216ee399a
--- /dev/null
+++ b/files/fr/web/javascript/reference/classes/class_static_initialization_blocks/index.md
@@ -0,0 +1,167 @@
+---
+title: Class static initialization blocks
+slug: Web/JavaScript/Reference/Classes/Class_static_initialization_blocks
+tags:
+ - Classes
+ - ECMAScript 2022
+ - JavaScript
+ - Language feature
+ - Static
+ - Reference
+ - Initialization
+browser-compat: javascript.classes.class_static_initialization_blocks
+---
+{{jsSidebar("Classes")}}
+
+**Class static initialization blocks** are a special feature of a {{jsxref("Statements/class", "class")}} that enable more flexible initialization of {{jsxref("Classes/static", "static")}} properties than can be achieved using per-field initialization.
+
+Static blocks allow statements to be evaluated during initialization, which allows initializations that (for example) include `try...catch` or set multiple fields from a single value.
+
+Initialization is performed in the context of the current class declaration, with privileged access to private state.
+This means that static blocks can also be used to share information between classes with instance private fields and other classes or functions declared in the same scope (analogous to "friend" classes in C++).
+
+{{EmbedInteractiveExample("pages/js/classes-static-initialization.html")}}
+
+## Syntax
+
+```js
+static { ... }
+```
+
+
+## Description
+
+A {{jsxref("Statements/class", "class")}} can have any number of `static {}` initialization blocks in its class body.
+These are evaluated, along with any interleaved static field initializers, in the order they are declared.
+Any static initialization of a super class is performed first, before that of its sub classes.
+
+The scope of the variables declared inside the static block is local to the block.
+Since `var`, `function`, `const` or `let` declared in a `static {}` initialization block are local to the block, any `var` declarations in the block are not hoisted.
+
+```js
+var y = 'Outer y';
+
+class A {
+ static field = 'Inner y';
+ static {
+ var y = this.field;
+ }
+}
+
+// var defined in static block is not hoisted
+console.log(y);
+// > 'Outer y'
+```
+
+The `this` inside a static block refers to the constructor object of the class.
+`super.<property>` can be used to access properties of a super class.
+Note however that it is a syntax error to call {{jsxref("Operators/super", "super()")}} in a class static initialization block, or to attempt to access arguments of the class constructor function.
+
+The scope of the static block is nested _within_ the lexical scope of the class body, and can access the private instance variables of the class.
+
+A static initialization block may not have decorators (the class itself may).
+
+
+## Examples
+
+### Multiple blocks
+
+The code below demonstrates a class with static initialization blocks and interleaved static field initializers.
+The output shows that the blocks and fields are evaluated in execution order.
+
+```js
+class MyClass {
+ static field1 = console.log('field1 called');
+ static {
+ console.log('Class static block #1 called');
+ }
+ static field2 = console.log('field2 called');
+ static {
+ console.log('Class static block #2 called');
+ }
+}
+
+/*
+> "field1 called"
+> "Class static block #1 called"
+> "field2 called"
+> "Class static block #2 called"
+*/
+```
+
+Note that any static initialization of a super class is performed first, before that of its sub classes.
+
+
+### Using `this` and `super.property`
+
+The `this` inside a static block refers to the constructor object of the class.
+This code shows how to access a public static field.
+
+```js
+class A {
+ static field = 'A static field';
+ static {
+ var y = this.field;
+ }
+}
+```
+
+The [`super.property`](/en-US/docs/Web/JavaScript/Reference/Operators/super) can be used inside a `static` block to reference properties of a super class.
+This includes static properties, as shown below:
+
+```js
+class A {
+ static fieldA = 'A.fieldA';
+}
+class B extends A {
+ static {
+ let x = super.fieldA;
+ // 'A.fieldA'
+ }
+}
+```
+
+### Access to private fields
+
+This example below shows how access can be granted to the private object of a class from an object outside the class (example from the [v8.dev blog](https://v8.dev/features/class-static-initializer-blocks#access-to-private-fields)):
+
+
+```js
+let getDPrivateField;
+
+class D {
+ #privateField;
+ constructor(v) {
+ this.#privateField = v;
+ }
+ static {
+ getDPrivateField = (d) => d.#privateField;
+ }
+}
+
+getDPrivateField(new D('private'));
+// > private
+```
+
+### Workarounds
+
+Prior to ES13 more complex static initialization might be achieved by using a static method that is called after the other properties to perform static initialization, or having a method that is external to the class that performs initialization tasks.
+
+In both cases the approach is less elegant, and does not grant access to private methods in the class.
+
+
+## Specifications
+
+{{Specifications}}
+
+## Browser compatibility
+
+{{Compat}}
+
+## See also
+
+- [Class static initialization blocks](https://v8.dev/features/class-static-initializer-blocks) (v8.dev blog)
+- [ES2022 feature: class static initialization blocks](https://2ality.com/2021/09/class-static-block.html) (2ality.com blog)
+- [Classes](/en-US/docs/Web/JavaScript/Reference/Classes)
+- {{jsxref("Operators/super", "super()")}}
+- [Object.prototype.constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor)
diff --git a/files/fr/web/javascript/reference/errors/hash_outside_class/index.md b/files/fr/web/javascript/reference/errors/hash_outside_class/index.md
new file mode 100644
index 0000000000..991af98458
--- /dev/null
+++ b/files/fr/web/javascript/reference/errors/hash_outside_class/index.md
@@ -0,0 +1,67 @@
+---
+title: 'SyntaxError: Unexpected ''#'' used outside of class body'
+slug: Web/JavaScript/Reference/Errors/Hash_outside_class
+translation_of: Web/JavaScript/Reference/Errors/Hash_outside_class
+---
+{{jsSidebar("Errors")}}
+
+L'expression JavaScript "Unexpected '#' used outside of class body" (qu'on peut traduire par «&nbsp;'#' inattendu en dehors d'un corps de classe&nbsp;») se produit lorsqu'un croisillon («&nbsp;#&nbsp;») est trouvé dans un contexte inattendu, notamment [en dehors d'une déclaration de classe](/fr/docs/Web/JavaScript/Reference/Classes/Private_class_fields). Les croisillons sont valides au début d'un fichier [comme commentaire d'interpréteur](/fr/docs/Web/JavaScript/Reference/Lexical_grammar), ou à l'intérieur d'une classe pour indiquer un champ privé. Vous pouvez également rencontrer cette erreur si vous oubliez d'encadrer un identifiant DOM entre quotes (la chaîne de caractères n'étant alors pas délimitée).
+
+## Message
+
+```js
+SyntaxError: Unexpected '#' used outside of class body.
+```
+
+## Type d'erreur
+
+[`SyntaxError`](/fr/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)
+
+## Quel est le problème&nbsp;?
+
+Un caractère \`#\` a été trouvé dans un contexte inattendu. Cela peut être à du code déplacé qui ne fait plus partie d'une classe, à un commentaire d'interpréteur (<i lang="en">hashbang</i>) situé sur une autre ligne que la première ou à l'oubli de quotes/guillemets autour d'un identifiant du DOM.
+
+## Exemples
+
+### Délimiteurs de chaîne manquants
+
+Pour chaque cas, on peut avoir une légère erreur qui produit cette exception. Par exemple&nbsp;:
+
+```js example-bad
+document.querySelector(#un-élément)
+```
+
+Pourra être corrigé avec&nbsp;:
+
+```js example-good
+document.querySelector("#un-élément")
+```
+
+### En dehors d'une classe
+
+```js example-bad
+class ClasseAvecChampPrivé {
+ #champPrivé
+
+ constructor() {
+ }
+}
+
+this.#champPrivé = 42
+```
+
+Cela pourra être corrigé en déplaçant le champ privé à l'intérieur de la classe&nbsp;:
+
+```js example-good
+class ClasseAvecChampPrivé {
+ #champPrivé
+
+ constructor() {
+ this.#champPrivé = 42
+ }
+}
+```
+
+## Voir aussi
+
+- [`SyntaxError`](/fr/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)