JavaScript syntax: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
m Dating maintenance tags: {{Citation needed}}
Symbol: modified the example to make it more clear; commented out a confusing snippet (if you understand it can you improve it?)
Line 410:
 
<syntaxhighlight lang="javascript">
var x = Symbol(1);
var y = Symbol(1);
x === y; // => false
 
arr=[x,y];
var symbolObject = {};
arr[x]=1;
var normalObject = {};
arr[y]=2; // x and y are unique keys for the array arr
 
arr[x]; // displays 1
// since x and y are unique,
arr[y]; // displays 2
// they can be used as unique keys in an object
x=Symbol(3);
symbolObject[x] = 1;
arr; // displays [Symbol(1),Symbol(1)]
symbolObject[y] = 2;
arr[x]; // is now undefined
 
x=Symbol(1);
arrsymbolObject[x]; // undefined=> 1
symbolObject[y]; // => 2
 
// as compared to normal numeric keys
normalObject[1] = 1;
normalObject[1] = 2; // overrides the value of 1
 
normalObject[1]; // => 2
 
// changing the value of x does not change the key stored in the object
x = Symbol(3);
arrsymbolObject[x]; // is now=> undefined
 
// changing x back just creates another unique Symbol
x = Symbol(1);
symbolObject[x]; // => undefined
</syntaxhighlight>
 
The<!-- Symbolwhat wrapperdoes alsothis providesmean accessand tohow ais it "variable free iterator."?
 
"The Symbol wrapper also provides access to a [sic] variable free iterator."
 
<syntaxhighlight lang="javascript">
Line 432 ⟶ 449:
while ((exv=ex.next().value)!=undefined) console.log(exv); // displays 1,2,3,4
</syntaxhighlight>
 
-->
 
==Native objects==