-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from int3l/main
Code examples for episode 552
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# [what is `Symbol` in js (intermediate)](https://youtu.be/gZBQ8LIR6D4) | ||
|
||
I recently was writing some JS and wondered what `Symbol` was -- and now that I know I'm sharing it with you | ||
|
||
## Interactive examples | ||
|
||
### JavaScript | ||
|
||
```javascript | ||
JSON.stringify({foo: 'hello'}) | ||
o = {toJSON: () => {return {hello: 'hello world'}}} | ||
JSON.stringify(o) | ||
|
||
Symbol | ||
|
||
o = {[Symbol.iterator]: function* () { yield 1; yield 2; yield 3; }} | ||
|
||
for (let i of o) { | ||
console.log(i); | ||
} | ||
|
||
o.iterator | ||
o[Symbol.iterator] | ||
|
||
let fooSymbol = Symbol('foo') | ||
fooSymbol | ||
|
||
Symbol('foo') == fooSymbol | ||
Symbol('foo') === fooSymbol | ||
|
||
let globalFoo = Symbol.for('foo') | ||
globalFoo == fooSymbol | ||
|
||
globalFoo == Symbol.for('foo') | ||
globalFoo === Symbol.for('foo') | ||
|
||
Symbol.keyFor(fooSymbol) | ||
Symbol.keyFor(Symbol.iterator) | ||
Symbol.keyFor(globalFoo) | ||
``` | ||
|
||
### Bash | ||
|
||
```bash | ||
node | ||
``` |