Skip to content

Commit

Permalink
Merge pull request #373 from int3l/main
Browse files Browse the repository at this point in the history
Code examples for episode 552
  • Loading branch information
asottile authored Sep 11, 2023
2 parents 6f41d7e + a56dc28 commit 75fff4b
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions sample_code/ep552/README.md
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
```

0 comments on commit 75fff4b

Please sign in to comment.