From 004b3c5fc8d71b68fcb019c9e0346bf80024dbbd Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:48:47 +0100 Subject: unslug nl: move --- .../global_objects/object/prototype/index.html | 241 --------------------- .../reference/global_objects/symbol/index.html | 205 ++++++++++++++++++ .../reference/global_objects/symbool/index.html | 205 ------------------ 3 files changed, 205 insertions(+), 446 deletions(-) delete mode 100644 files/nl/web/javascript/reference/global_objects/object/prototype/index.html create mode 100644 files/nl/web/javascript/reference/global_objects/symbol/index.html delete mode 100644 files/nl/web/javascript/reference/global_objects/symbool/index.html (limited to 'files/nl/web/javascript/reference/global_objects') diff --git a/files/nl/web/javascript/reference/global_objects/object/prototype/index.html b/files/nl/web/javascript/reference/global_objects/object/prototype/index.html deleted file mode 100644 index 8fcfcbfa59..0000000000 --- a/files/nl/web/javascript/reference/global_objects/object/prototype/index.html +++ /dev/null @@ -1,241 +0,0 @@ ---- -title: Object.prototype -slug: Web/JavaScript/Reference/Global_Objects/Object/prototype -tags: - - JavaScript - - Object - - Property -translation_of: Web/JavaScript/Reference/Global_Objects/Object -translation_of_original: Web/JavaScript/Reference/Global_Objects/Object/prototype ---- -
{{JSRef}}
- -

De Object.prototype eigenschap vertegenwoordigt het {{jsxref("Object")}} prototype object.

- -

{{js_property_attributes(0, 0, 0)}}

- -

Beschrijving

- -

Alle objecten in JavaScript zijn afstammelingen van het {{jsxref("Object")}}; alle objecten erven methode's en eigenschappen van Object.prototype, althans kunnen ze overschreden worden (behalve een Object met een null prototype, i.e Object.create(null)). Bijvoorbeeld, een andere constructors' prototypes overschrijden de constructor eigenschap en voorzien hun eigen  {{jsxref("Object.prototype.toString()", "toString()")}} methode's.

- -

Veranderingen in het Object prototype object zijn zichtbaar voor alle objecten door prototype chaining, tenzij de eigenschappen en methode's onderworpen door deze veranderingen worden overschreden verderop de prototype chain. Dit voorziet een vrij krachtig althans potentieel gevaarlijk mechanisme om het gedrag van een object te overschrijden of aan te vullen.

- -

Eigenschappen

- -
-
{{jsxref("Object.prototype.constructor")}}
-
Beschrijft de functie dat het object's prototype aanmaakt.
-
{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}
-
Wijst aan het object welke was bebruikt als prototype wanneer het object was geinstantieerd.
-
{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}
-
Laat het toe om een functie te definieeren dat zal worden uitgevoerd wanneer een ongedefinieerd lid van een object word geroepen als een methode. 
-
{{jsxref("Object.prototype.count","Object.prototype.__count__")}} {{obsolete_inline}}
-
Used to return the number of enumerable properties directly on a user-defined object, but has been removed.
-
{{jsxref("Object.prototype.parent","Object.prototype.__parent__")}} {{obsolete_inline}}
-
Used to point to an object's context, but has been removed.
-
- -

Methode's

- -
-
{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
Associeert een functie met een eigenschap dat, wanneer toegankelijk, een functie uitvoerd en zijn keert zijn return waarde terug.
-
{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
Associeert een functie met een eigenschap dat, wanneer gezet, een functie uitvoerd dat de eigenchap veranderd.
-
{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
Keert de functie geassocieert met de gespecificieerde eigenschap door de {{jsxref("Object.prototype.__defineGetter__()", "__defineGetter__()")}} methode.
-
{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
Keert de functie geassocieert met de gespecificieerde eigenschap door de {{jsxref("Object.prototype.__defineSetter__()", "__defineSetter__()")}} method.
-
{{jsxref("Object.prototype.hasOwnProperty()")}}
-
Keert een boolean terug die aanwijst of een object dat de eigenschap bevat als een directe eigenschap van dat object en niet wordt geerfd door de prototype chain.
-
{{jsxref("Object.prototype.isPrototypeOf()")}}
-
Keert een boolean waarde terug die aanwijst of het object in de prototype chain zit van het object van waaruit deze methode is geroepen.
-
{{jsxref("Object.prototype.propertyIsEnumerable()")}}
-
Keert een boolean waarde terug die aanwijst of de ECMAScript [[Enumerable]] attribute is gezet.
-
{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}
-
Keert een string terug die de bron van een object zijn literal, die het object waarop deze methode word op geroepen represedenteerd; je kan deze waarde gebruiken om een niew object te maken.
-
{{jsxref("Object.prototype.toLocaleString()")}}
-
Roept {{jsxref("Object.toString", "toString()")}}.
-
{{jsxref("Object.prototype.toString()")}}
-
Keert een string representatie terug van het object.
-
{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}
-
Verwijderd een watchpoint van een eigenschap van het object.
-
{{jsxref("Object.prototype.valueOf()")}}
-
Keert een primitive waarde terug van het gespecifieerde object.
-
{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}
-
Voegt een watchpoint toe aan de eigenschap van het object.
-
{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}
-
Used to evaluate a string of JavaScript code in the context of the specified object, but has been removed.
-
- -

Voorbeelden

- -

Als het gedrag van bestaande Object.prototype's methode's worden veranderd, overweeg code te injecteren door je uitbreiding in te wikkelen voor of achter de bestaande logica. Bijvoorbeeld, deze (ongeteste) code zal onvoorwaardelijk aangepaste logica uitvoeren vooralleer de ingebouwde logica of anderman's uitbreiding word uitgevoerd.

- -

Wanneer een functie is geroepen zullen de argumenten worden gehouden in de array-achtige "variable" van de argumenten. Bijvoorbeeld, in de call "myFn(a, b, c)", zullen de argumenten binnenin myFn's lichaam 3 array elementen bevatten die coressponderen tot (a, b, c).  Wanneer prototype's met haken worden bijgewerkt, voer simpelweg deze & de argementen (de call toestand) toe aan het huidig gedrag door apply() te roepen op de functie. Dit patroon can worden gebruikt voor elk prototype, zoals Node.prototype, Function.prototype, etc.

- -

var current = Object.prototype.valueOf; // Since my property "-prop-value" is cross-cutting and isn't always // on the same prototype chain, I want to modify Object.prototype: Object.prototype.valueOf = function() { if (this.hasOwnProperty("-prop-value") {   return this["-prop-value"];   } else {   // It doesn't look like one of my objects, so let's fall back on   // the default behavior by reproducing the current behavior as best we can.   // The apply behaves like "super" in some other languages.   // Even though valueOf() doesn't take arguments, some other hook may.   return current.apply(this, arguments);   } }

- -

Doordat JavaScript geen sub-classe object bevat, prototype is een handige workaround om een "base class" object aan te maken van bepaalde functie's die zich gedragen als objecten. Bijvoorbeeld:

- -
var Person = function() {
-  this.canTalk = true;
-};
-
-Person.prototype.greet = function() {
-  if (this.canTalk) {
-    console.log('Hi, I am ' + this.name);
-  }
-};
-
-var Employee = function(name, title) {
-  Person.call(this);
-  this.name = name;
-  this.title = title;
-};
-
-Employee.prototype = Object.create(Person.prototype);
-Employee.prototype.constructor = Employee;
-
-Employee.prototype.greet = function() {
-  if (this.canTalk) {
-    console.log('Hi, I am ' + this.name + ', the ' + this.title);
-  }
-};
-
-var Customer = function(name) {
-  Person.call(this);
-  this.name = name;
-};
-
-Customer.prototype = Object.create(Person.prototype);
-Customer.prototype.constructor = Customer;
-
-var Mime = function(name) {
-  Person.call(this);
-  this.name = name;
-  this.canTalk = false;
-};
-
-Mime.prototype = Object.create(Person.prototype);
-Mime.prototype.constructor = Mime;
-
-var bob = new Employee('Bob', 'Builder');
-var joe = new Customer('Joe');
-var rg = new Employee('Red Green', 'Handyman');
-var mike = new Customer('Mike');
-var mime = new Mime('Mime');
-
-bob.greet();
-// Hi, I am Bob, the Builder
-
-joe.greet();
-// Hi, I am Joe
-
-rg.greet();
-// Hi, I am Red Green, the Handyman
-
-mike.greet();
-// Hi, I am Mike
-
-mime.greet();
-
- -

Specificaties

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificatieStatusCommentaar
{{SpecName('ES1')}}{{Spec2('ES1')}}Initiale definitie. Geimplemented in JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-object.prototype', 'Object.prototype')}}{{Spec2('ESDraft')}} 
- -

Browser ondersteuning

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
SoortChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basis Ondersteuning{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
SoortAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
- - - - - - - -
Basis Ondersteuning 
-
{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -

See also

- - diff --git a/files/nl/web/javascript/reference/global_objects/symbol/index.html b/files/nl/web/javascript/reference/global_objects/symbol/index.html new file mode 100644 index 0000000000..f0777451c8 --- /dev/null +++ b/files/nl/web/javascript/reference/global_objects/symbol/index.html @@ -0,0 +1,205 @@ +--- +title: Symbool +slug: Web/JavaScript/Reference/Global_Objects/Symbool +tags: + - ECMAScript 2015 + - JavaScript + - Klasse + - Symbool +translation_of: Web/JavaScript/Reference/Global_Objects/Symbol +--- +
{{JSRef}}
+ +

Het gegevenstype symbool is een primitief gegevenstype. De Symbol() functie geeft een waarde terug (returns a value) van het type symbool, heeft statische eigenschappen die verscheidene leden van ingebouwde objecten blootstelt, heeft statische methoden die het globale symbolregister blootstellen en vertegenwoordigd een ingebouwde objectklasse. Maar is onvolledig als een constructor, omdat het niet de "new Symbol()" syntaxis ondersteund.

+ +

Elke waarde teruggekregen van Symbol() is uniek. Zo'n teruggekregen waarde kan, bijvoorbeeld, gebruikt worden als identificatiemiddel voor objecteigenschappen; het primaire doel van dit gegevenstype. Hoewel er andere use-cases zijn, zoals het beschikbaar maken van ondoorzichtige gegevenstypen of als algemeen uniek identificatiemiddel. Meer uitleg over het doel en gebruik van het symbool is te vinden in de woordenlijst.

+ +

Beschrijving

+ +

Om een nieuw primitief symbool te creëren, schrijf je Symbol() met een optionele String als beschrijving:

+ +
let sym1 = Symbol()
+let sym2 = Symbol('foo')
+let sym3 = Symbol('foo')
+
+ +

De bovenstaande code creëert drie nieuwe symbolen. Let er op dat Symbol("foo") niet de string "foo" omzet naar een symbool maar dat het telkens een nieuw uniek symbool creëert:

+ +
Symbol('foo') === Symbol('foo')  // false
+
+ +

De volgende syntaxis met de {{jsxref("Operators/new", "new")}} operator zal een {{jsxref("TypeError")}}: afwerpen:

+ +
let sym = new Symbol()  // TypeError
+
+ +

Dit behoed auteurs ervoor om nadrukkelijk een Symbol wrapper-object te creëren in plaats van een nieuwe symboolwaarde. Terwijl normaal gesproken primitieve gegevenstypen wel gemaakt kunnen worden met een wrapper-object. (Zoals: new Boolean, new String en new Number).

+ +

Als je echt een Symbol wrapper-object wilt maken, kun je dit doen met de Object() functie:

+ +
let sym = Symbol('foo')
+typeof sym      // "symbol"
+let symObj = Object(sym)
+typeof symObj   // "object"
+
+ +

Gedeelde symbolen in het globale symboolregister

+ +

De bovenstaande syntaxis, die gebruik maakt van de Symbol() functie, creëert alleen niet een globaal symbool dat te gebruiken is door je gehele codebase. Om symbolen te creëren die door al je bestanden en zelfs door je realms (met elk hun eigen globale scope) te gebruiken zijn; gebruik je de methoden {{jsxref("Symbol.for()")}} en {{jsxref("Symbol.keyFor()")}}. Om, respectievelijksymbolen in het globale symbolenregister aan te maken en terug te krijgen.

+ +

Symbooleigenschappen vinden in objecten

+ +

De methode {{jsxref("Object.getOwnPropertySymbols()")}} geeft een array met symbolen terug en laat je symbooleigenschappen vinden in een opgegeven object. Let er op dat elk object geïnitialiseerd wordt zonder eigen symbooleigenschappen, dus deze array zal leeg zijn tenzij je een symbool als eigenschap hebt gegeven aan een object. 

+ +

Constructor

+ +
+
Symbol()
+
De  Symbol() constructor geeft een waarde terug van het type symbol, maar is incompleet als een constructor omdat het niet de "new Symbol()" syntaxis ondersteund.
+
+ +

Static properties

+ +
+
{{jsxref("Symbol.asyncIterator")}}
+
A method that returns the default AsyncIterator for an object. Used by for await...of.
+
{{jsxref("Symbol.hasInstance")}}
+
A method determining if a constructor object recognizes an object as its instance. Used by {{jsxref("Operators/instanceof", "instanceof")}}.
+
{{jsxref("Symbol.isConcatSpreadable")}}
+
A Boolean value indicating if an object should be flattened to its array elements. Used by {{jsxref("Array.prototype.concat()")}}.
+
{{jsxref("Symbol.iterator")}}
+
A method returning the default iterator for an object. Used by for...of.
+
{{jsxref("Symbol.match")}}
+
A method that matches against a string, also used to determine if an object may be used as a regular expression. Used by {{jsxref("String.prototype.match()")}}.
+
{{jsxref("Symbol.matchAll")}}
+
A method that returns an iterator, that yields matches of the regular expression against a string. Used by {{jsxref("String.prototype.matchAll()")}}.
+
{{jsxref("Symbol.replace")}}
+
A method that replaces matched substrings of a string. Used by {{jsxref("String.prototype.replace()")}}.
+
{{jsxref("Symbol.search")}}
+
A method that returns the index within a string that matches the regular expression. Used by {{jsxref("String.prototype.search()")}}.
+
{{jsxref("Symbol.split")}}
+
A method that splits a string at the indices that match a regular expression. Used by {{jsxref("String.prototype.split()")}}.
+
{{jsxref("Symbol.species")}}
+
A constructor function that is used to create derived objects.
+
{{jsxref("Symbol.toPrimitive")}}
+
A method converting an object to a primitive value.
+
{{jsxref("Symbol.toStringTag")}}
+
A string value used for the default description of an object. Used by {{jsxref("Object.prototype.toString()")}}.
+
{{jsxref("Symbol.unscopables")}}
+
An object value of whose own and inherited property names are excluded from the with environment bindings of the associated object.
+
+ +

Static methods

+ +
+
{{jsxref("Symbol.for()", "Symbol.for(key)")}}
+
Searches for existing symbols with the given key and returns it if found. Otherwise a new symbol gets created in the global symbol registry with key.
+
{{jsxref("Symbol.keyFor", "Symbol.keyFor(sym)")}}
+
Retrieves a shared symbol key from the global symbol registry for the given symbol.
+
+ +

Instance properties

+ +
+
{{jsxref("Symbol.prototype.description")}}
+
A read-only string containing the description of the symbol.
+
+ +

Instance methods

+ +
+
{{jsxref("Symbol.prototype.toSource()")}}
+
Returns a string containing the source of the {{jsxref("Global_Objects/Symbol", "Symbol")}} object. Overrides the {{jsxref("Object.prototype.toSource()")}} method.
+
{{jsxref("Symbol.prototype.toString()")}}
+
Returns a string containing the description of the Symbol. Overrides the {{jsxref("Object.prototype.toString()")}} method.
+
{{jsxref("Symbol.prototype.valueOf()")}}
+
Returns the primitive value of the {{jsxref("Symbol")}} object. Overrides the {{jsxref("Object.prototype.valueOf()")}} method.
+
{{jsxref("Symbol.prototype.@@toPrimitive()", "Symbol.prototype[@@toPrimitive]")}}
+
Returns the primitive value of the {{jsxref("Symbol")}} object.
+
+ +

Examples

+ +

Using the typeof operator with symbols

+ +

The {{jsxref("Operators/typeof", "typeof")}} operator can help you to identify symbols.

+ +
typeof Symbol() === 'symbol'
+typeof Symbol('foo') === 'symbol'
+typeof Symbol.iterator === 'symbol'
+
+ +

Symbol type conversions

+ +

Some things to note when working with type conversion of symbols.

+ + + +

Symbols and for...in iteration

+ +

Symbols are not enumerable in for...in iterations. In addition, {{jsxref("Object.getOwnPropertyNames()")}} will not return symbol object properties, however, you can use {{jsxref("Object.getOwnPropertySymbols()")}} to get these.

+ +
let obj = {}
+
+obj[Symbol('a')] = 'a'
+obj[Symbol.for('b')] = 'b'
+obj['c'] = 'c'
+obj.d = 'd'
+
+for (let i in obj) {
+   console.log(i)  // logs "c" and "d"
+}
+ +

Symbols and JSON.stringify()

+ +

Symbol-keyed properties will be completely ignored when using JSON.stringify():

+ +
JSON.stringify({[Symbol('foo')]: 'foo'})
+// '{}'
+
+ +

For more details, see {{jsxref("JSON.stringify()")}}.

+ +

Symbol wrapper objects as property keys

+ +

When a Symbol wrapper object is used as a property key, this object will be coerced to its wrapped symbol:

+ +
let sym = Symbol('foo')
+let obj = {[sym]: 1}
+obj[sym]             // 1
+obj[Object(sym)]     // still 1
+
+ +

Specifications

+ + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-symbol-objects', 'Symbol')}}
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Symbol")}}

+ +

See also

+ + diff --git a/files/nl/web/javascript/reference/global_objects/symbool/index.html b/files/nl/web/javascript/reference/global_objects/symbool/index.html deleted file mode 100644 index f0777451c8..0000000000 --- a/files/nl/web/javascript/reference/global_objects/symbool/index.html +++ /dev/null @@ -1,205 +0,0 @@ ---- -title: Symbool -slug: Web/JavaScript/Reference/Global_Objects/Symbool -tags: - - ECMAScript 2015 - - JavaScript - - Klasse - - Symbool -translation_of: Web/JavaScript/Reference/Global_Objects/Symbol ---- -
{{JSRef}}
- -

Het gegevenstype symbool is een primitief gegevenstype. De Symbol() functie geeft een waarde terug (returns a value) van het type symbool, heeft statische eigenschappen die verscheidene leden van ingebouwde objecten blootstelt, heeft statische methoden die het globale symbolregister blootstellen en vertegenwoordigd een ingebouwde objectklasse. Maar is onvolledig als een constructor, omdat het niet de "new Symbol()" syntaxis ondersteund.

- -

Elke waarde teruggekregen van Symbol() is uniek. Zo'n teruggekregen waarde kan, bijvoorbeeld, gebruikt worden als identificatiemiddel voor objecteigenschappen; het primaire doel van dit gegevenstype. Hoewel er andere use-cases zijn, zoals het beschikbaar maken van ondoorzichtige gegevenstypen of als algemeen uniek identificatiemiddel. Meer uitleg over het doel en gebruik van het symbool is te vinden in de woordenlijst.

- -

Beschrijving

- -

Om een nieuw primitief symbool te creëren, schrijf je Symbol() met een optionele String als beschrijving:

- -
let sym1 = Symbol()
-let sym2 = Symbol('foo')
-let sym3 = Symbol('foo')
-
- -

De bovenstaande code creëert drie nieuwe symbolen. Let er op dat Symbol("foo") niet de string "foo" omzet naar een symbool maar dat het telkens een nieuw uniek symbool creëert:

- -
Symbol('foo') === Symbol('foo')  // false
-
- -

De volgende syntaxis met de {{jsxref("Operators/new", "new")}} operator zal een {{jsxref("TypeError")}}: afwerpen:

- -
let sym = new Symbol()  // TypeError
-
- -

Dit behoed auteurs ervoor om nadrukkelijk een Symbol wrapper-object te creëren in plaats van een nieuwe symboolwaarde. Terwijl normaal gesproken primitieve gegevenstypen wel gemaakt kunnen worden met een wrapper-object. (Zoals: new Boolean, new String en new Number).

- -

Als je echt een Symbol wrapper-object wilt maken, kun je dit doen met de Object() functie:

- -
let sym = Symbol('foo')
-typeof sym      // "symbol"
-let symObj = Object(sym)
-typeof symObj   // "object"
-
- -

Gedeelde symbolen in het globale symboolregister

- -

De bovenstaande syntaxis, die gebruik maakt van de Symbol() functie, creëert alleen niet een globaal symbool dat te gebruiken is door je gehele codebase. Om symbolen te creëren die door al je bestanden en zelfs door je realms (met elk hun eigen globale scope) te gebruiken zijn; gebruik je de methoden {{jsxref("Symbol.for()")}} en {{jsxref("Symbol.keyFor()")}}. Om, respectievelijksymbolen in het globale symbolenregister aan te maken en terug te krijgen.

- -

Symbooleigenschappen vinden in objecten

- -

De methode {{jsxref("Object.getOwnPropertySymbols()")}} geeft een array met symbolen terug en laat je symbooleigenschappen vinden in een opgegeven object. Let er op dat elk object geïnitialiseerd wordt zonder eigen symbooleigenschappen, dus deze array zal leeg zijn tenzij je een symbool als eigenschap hebt gegeven aan een object. 

- -

Constructor

- -
-
Symbol()
-
De  Symbol() constructor geeft een waarde terug van het type symbol, maar is incompleet als een constructor omdat het niet de "new Symbol()" syntaxis ondersteund.
-
- -

Static properties

- -
-
{{jsxref("Symbol.asyncIterator")}}
-
A method that returns the default AsyncIterator for an object. Used by for await...of.
-
{{jsxref("Symbol.hasInstance")}}
-
A method determining if a constructor object recognizes an object as its instance. Used by {{jsxref("Operators/instanceof", "instanceof")}}.
-
{{jsxref("Symbol.isConcatSpreadable")}}
-
A Boolean value indicating if an object should be flattened to its array elements. Used by {{jsxref("Array.prototype.concat()")}}.
-
{{jsxref("Symbol.iterator")}}
-
A method returning the default iterator for an object. Used by for...of.
-
{{jsxref("Symbol.match")}}
-
A method that matches against a string, also used to determine if an object may be used as a regular expression. Used by {{jsxref("String.prototype.match()")}}.
-
{{jsxref("Symbol.matchAll")}}
-
A method that returns an iterator, that yields matches of the regular expression against a string. Used by {{jsxref("String.prototype.matchAll()")}}.
-
{{jsxref("Symbol.replace")}}
-
A method that replaces matched substrings of a string. Used by {{jsxref("String.prototype.replace()")}}.
-
{{jsxref("Symbol.search")}}
-
A method that returns the index within a string that matches the regular expression. Used by {{jsxref("String.prototype.search()")}}.
-
{{jsxref("Symbol.split")}}
-
A method that splits a string at the indices that match a regular expression. Used by {{jsxref("String.prototype.split()")}}.
-
{{jsxref("Symbol.species")}}
-
A constructor function that is used to create derived objects.
-
{{jsxref("Symbol.toPrimitive")}}
-
A method converting an object to a primitive value.
-
{{jsxref("Symbol.toStringTag")}}
-
A string value used for the default description of an object. Used by {{jsxref("Object.prototype.toString()")}}.
-
{{jsxref("Symbol.unscopables")}}
-
An object value of whose own and inherited property names are excluded from the with environment bindings of the associated object.
-
- -

Static methods

- -
-
{{jsxref("Symbol.for()", "Symbol.for(key)")}}
-
Searches for existing symbols with the given key and returns it if found. Otherwise a new symbol gets created in the global symbol registry with key.
-
{{jsxref("Symbol.keyFor", "Symbol.keyFor(sym)")}}
-
Retrieves a shared symbol key from the global symbol registry for the given symbol.
-
- -

Instance properties

- -
-
{{jsxref("Symbol.prototype.description")}}
-
A read-only string containing the description of the symbol.
-
- -

Instance methods

- -
-
{{jsxref("Symbol.prototype.toSource()")}}
-
Returns a string containing the source of the {{jsxref("Global_Objects/Symbol", "Symbol")}} object. Overrides the {{jsxref("Object.prototype.toSource()")}} method.
-
{{jsxref("Symbol.prototype.toString()")}}
-
Returns a string containing the description of the Symbol. Overrides the {{jsxref("Object.prototype.toString()")}} method.
-
{{jsxref("Symbol.prototype.valueOf()")}}
-
Returns the primitive value of the {{jsxref("Symbol")}} object. Overrides the {{jsxref("Object.prototype.valueOf()")}} method.
-
{{jsxref("Symbol.prototype.@@toPrimitive()", "Symbol.prototype[@@toPrimitive]")}}
-
Returns the primitive value of the {{jsxref("Symbol")}} object.
-
- -

Examples

- -

Using the typeof operator with symbols

- -

The {{jsxref("Operators/typeof", "typeof")}} operator can help you to identify symbols.

- -
typeof Symbol() === 'symbol'
-typeof Symbol('foo') === 'symbol'
-typeof Symbol.iterator === 'symbol'
-
- -

Symbol type conversions

- -

Some things to note when working with type conversion of symbols.

- - - -

Symbols and for...in iteration

- -

Symbols are not enumerable in for...in iterations. In addition, {{jsxref("Object.getOwnPropertyNames()")}} will not return symbol object properties, however, you can use {{jsxref("Object.getOwnPropertySymbols()")}} to get these.

- -
let obj = {}
-
-obj[Symbol('a')] = 'a'
-obj[Symbol.for('b')] = 'b'
-obj['c'] = 'c'
-obj.d = 'd'
-
-for (let i in obj) {
-   console.log(i)  // logs "c" and "d"
-}
- -

Symbols and JSON.stringify()

- -

Symbol-keyed properties will be completely ignored when using JSON.stringify():

- -
JSON.stringify({[Symbol('foo')]: 'foo'})
-// '{}'
-
- -

For more details, see {{jsxref("JSON.stringify()")}}.

- -

Symbol wrapper objects as property keys

- -

When a Symbol wrapper object is used as a property key, this object will be coerced to its wrapped symbol:

- -
let sym = Symbol('foo')
-let obj = {[sym]: 1}
-obj[sym]             // 1
-obj[Object(sym)]     // still 1
-
- -

Specifications

- - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#sec-symbol-objects', 'Symbol')}}
- -

Browser compatibility

- - - -

{{Compat("javascript.builtins.Symbol")}}

- -

See also

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