Slide 1

Slide 1 text

what we talk about @vaidehijoshi when we talk about the javascript object model

Slide 2

Slide 2 text

(we’re hiring!)

Slide 3

Slide 3 text

{}

Slide 4

Slide 4 text

let cat = {};

Slide 5

Slide 5 text

let cat = { sound: “meow” };

Slide 6

Slide 6 text

cat.sound cat[“sound”]

Slide 7

Slide 7 text

property descriptors

Slide 8

Slide 8 text

meta objects for all the properties on an object

Slide 9

Slide 9 text

Object.getOwnPropertyDescriptor( cat, “sound” ); { value: "meow", writable: true, enumerable: true, configurable: true } cat.sound

Slide 10

Slide 10 text

{ value: "meow", writable: true, enumerable: true, configurable: true } cat.sound

Slide 11

Slide 11 text

{ value: "meow", writable: true, enumerable: true, configurable: true } cat.sound

Slide 12

Slide 12 text

value writable cat.sound data property

Slide 13

Slide 13 text

data property has a value, which might be editable

Slide 14

Slide 14 text

data property object string number

Slide 15

Slide 15 text

let cat = { sayHi: function() { return “Hi!”; } }; cat.sayHi = function() { return “No, I will not say hi. I am a cat.”; };

Slide 16

Slide 16 text

Object.getOwnPropertyDescriptor( cat, “sayHi” ); { value: ƒ(), writable: true, enumerable: true, configurable: true }

Slide 17

Slide 17 text

{ value: "meow", writable: true, enumerable: true, configurable: true } cat.sound

Slide 18

Slide 18 text

enumerable configurable

Slide 19

Slide 19 text

enumerable configurable for…in Object.keys()

Slide 20

Slide 20 text

enumerable configurable

Slide 21

Slide 21 text

getters & setters

Slide 22

Slide 22 text

let cat = { sound: “meow”, }; get loudSound() { return this.sound.toUppercase() + Math.random(); }

Slide 23

Slide 23 text

Object.getOwnPropertyDescriptor( cat, “loudSound” ); { get: ƒ(), set: undefined, enumerable: true, configurable: true }

Slide 24

Slide 24 text

get set accessor property cat.loudSound

Slide 25

Slide 25 text

has a pair of getter + setter functions accessor property

Slide 26

Slide 26 text

set ƒ() get ƒ() accessor property

Slide 27

Slide 27 text

cat.loudSound() > "MEOW0.2812205663401923" get loudSound() { return this.sound.toUppercase() + Math.random(); } cat.loudSound() > "MEOW0.41972556946839834" cat.loudSound() > “MEOW0.5109470702014649"

Slide 28

Slide 28 text

cat.loudSound > "MEOW0.2812205663401923" cat.loudSound > "MEOW0.41972556946839834" cat.loudSound > “MEOW0.5109470702014649"

Slide 29

Slide 29 text

data property accessor property

Slide 30

Slide 30 text

new properties can be added to the object extensible

Slide 31

Slide 31 text

enumerable configurable

Slide 32

Slide 32 text

no removing or adding of properties, but continue to write Object.seal()

Slide 33

Slide 33 text

let cat = { sound: “meow” }; Object.seal(cat); { configurable: false, enumerable: true, value: “meow”, writable: true }

Slide 34

Slide 34 text

no removing or adding of properties and no writing to properties, either! Object.freeze()

Slide 35

Slide 35 text

let cat = { sound: “meow” }; Object.freeze(cat); { configurable: false, enumerable: true, value: “meow”, writable: false }

Slide 36

Slide 36 text

Object.freeze(cat); A getter’s value isn’t going to be frozen Properties that have other objects as their value need to be frozen

Slide 37

Slide 37 text

enumerable

Slide 38

Slide 38 text

only an object’s own properties are enumerable* *its parent’s (prototype) properties will not be enumerable

Slide 39

Slide 39 text

Object. getOwnPropertyDescriptors([]) { length: { value: 0, writable: true, enumerable: false, configurable: false }, __proto__: Object } we don’t want these to show up in a loop!

Slide 40

Slide 40 text

When possible, use Reflect, not Object! Reflect

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

objects are complex! but also super cool don’t be afraid to peek under the hood at how they work!

Slide 43

Slide 43 text

baseds.org @vaidehijoshi thank you! basecs.org