objects, objects everywhere

70
Objects, Objects Everywhere Mike Pack @zombidev

Upload: mikepack

Post on 27-Jun-2015

534 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Objects, Objects Everywhere

Objects, Objects Everywhere

Mike Pack@zombidev

Page 2: Objects, Objects Everywhere

Let’s Talk Objects

Behavior+

State

Page 3: Objects, Objects Everywhere

Let’s Talk ObjectsBehavior

WalkTalkEat

Move LegsMove Lips

Ingest Food

I’m a person, what can I do?

Could also be defined as:

Page 4: Objects, Objects Everywhere

Let’s Talk ObjectsBehavior

Level of abstraction matters!Behavioral abstraction varies per domain.

Page 5: Objects, Objects Everywhere

Let’s Talk ObjectsState

Mike PackMale

Brown

NameGender

Hair Color

I’m a person, who am I currently?

Therefore, I have attributes:

Page 6: Objects, Objects Everywhere

Let’s Talk ObjectsState

States changeWhen I’m 70, my attributes might be:

Mike PackMaleGrey

Page 7: Objects, Objects Everywhere

Let’s Talk Objects

Behavior changes too!I just learned to jump, now my behavior is:

WalkTalkEat

Jump

Page 8: Objects, Objects Everywhere

Let’s Talk Objects

Behavior+

State

Page 9: Objects, Objects Everywhere

HTML

<div id=”name”> Mike</div>

Is this an object?

Page 10: Objects, Objects Everywhere

HTML

<div id=”name”> Mike</div>

Is this an object?

Attribute Name

Page 11: Objects, Objects Everywhere

HTML

<div id=”name”> Mike</div>

Is this an object?

State

Attribute Name

Page 12: Objects, Objects Everywhere

HTML

<div id=”name”> Mike</div>

Is this an object?

State

Attribute Name

Where’s the beef?

Page 13: Objects, Objects Everywhere

HTML

<div id=”name”> Mike</div>

Is this an object?

State

Attribute Name

Where’s the behavior?

Page 14: Objects, Objects Everywhere

HTML<div id=”name” onclick=”...”> Mike</div>

Is this an object?

Page 15: Objects, Objects Everywhere

HTML<div id=”name” onclick=”...”> Mike</div>

Is this an object?

Attribute Name

State

Behavior!

Page 16: Objects, Objects Everywhere

HTML

HTML is object oriented(albeit, not for purist)

HTML does not exhibit other properties of an OO systems:- Inheritance- Polymorphism- etc

Page 17: Objects, Objects Everywhere

HTML

Don’t use onclick=”...”However, without onclick, HTML does not exhibit behavior.

Page 18: Objects, Objects Everywhere

CSS

a { color: red;}

Is this an object?

Page 19: Objects, Objects Everywhere

a { color: red;}

CSS

Is this an object?

Attribute Name

Page 20: Objects, Objects Everywhere

a { color: red;}

CSS

Is this an object?

State

Attribute Name

Page 21: Objects, Objects Everywhere

a { color: red;}

CSS

Is this an object?

State

Attribute Name

Where’s the beef?

Page 22: Objects, Objects Everywhere

CSSa { color: red; &:hover { color: blue; }}

Is this an object?

Page 23: Objects, Objects Everywhere

a { color: red; &:hover { color: blue; }}

CSS

Is this an object?

Attribute Name

State

Behavior!

Page 24: Objects, Objects Everywhere

a { color: red; &:hover { color: blue; }}

CSS

State Change

Behavior

Behavior changes state.

Page 25: Objects, Objects Everywhere

CSS

CSS is object oriented(not for purist)

CSS does not exhibit other properties of an OO systems:- Inheritance- Polymorphism- etc

Page 26: Objects, Objects Everywhere

JavaScript

Is this an object?

var book = { title: “The Art of War”};

Page 27: Objects, Objects Everywhere

JavaScript

var book = { title: “The Art of War”};

Is this an object?

Attribute Name

Page 28: Objects, Objects Everywhere

JavaScript

var book = { title: “The Art of War”};

Is this an object?

Attribute Name

State

Page 29: Objects, Objects Everywhere

JavaScript

var book = { title: “The Art of War”};

Is this an object?

Attribute Name

StateWhere’s the beef?

Page 30: Objects, Objects Everywhere

JavaScript

var book = { title: “The Art of War”};

Is this an object?

Attribute Name

StateIt’s implicit!Where’s the beef?

Page 31: Objects, Objects Everywhere

JavaScript var book = { title: “The Art of War”, constructor: function() {...}, hasOwnProperty: function() {...}, ...};

Implicit behavior.

Page 32: Objects, Objects Everywhere

JavaScript

JavaScript is object oriented(of course)

Page 33: Objects, Objects Everywhere

JavaScript

It’s not entirely object orientedJavaScript has primitives, too.

Page 34: Objects, Objects Everywhere

JavaScript

If it’s an object, typeof knowstypeof {} === ‘object’ //=> true

Page 35: Objects, Objects Everywhere

JavaScript

Integers are not objectstypeof 1 === ‘object’ //=> false

Page 36: Objects, Objects Everywhere

JavaScript

undefined is not an objecttypeof undefined === ‘object’ //=> false

Page 37: Objects, Objects Everywhere

JavaScript

Literals are not always primitivestypeof [] === ‘object’ //=> true

Page 38: Objects, Objects Everywhere

JavaScript

typeof might not return objecttypeof true === ‘object’ //=> false

But booleans are objects!typeof true === ‘boolean’ //=> true

new Boolean(1).valueOf() === true //=> true

Page 39: Objects, Objects Everywhere

JavaScript

Careful, typeof can bitetypeof null === ‘object’ //=> true

new Null() //=> error

Page 40: Objects, Objects Everywhere

Ruby

1

Is this an object?

Page 41: Objects, Objects Everywhere

Ruby

11.real #=> 1

Is this an object?

State (accessor)

Page 42: Objects, Objects Everywhere

Ruby

11.real #=> 11 + 1 #=> 2Is this an object?

State (accessor)

Page 43: Objects, Objects Everywhere

Ruby 1

1.real #=> 11 + 1 #=> 21.+(1) #=> 2Is this an object?

Behavior

State (accessor)

Page 44: Objects, Objects Everywhere

Ruby

Ruby is object oriented

Page 45: Objects, Objects Everywhere

Ruby

Everything is an object

Page 46: Objects, Objects Everywhere

Ruby

Literal arrays are new objects[].object_id #=> 70318249770140[].object_id #=> 70318249594160

Page 47: Objects, Objects Everywhere

Ruby

Literal strings are new objects‘slurpy’.object_id #=> 70318249561400‘slurpy’.object_id #=> 70318249500340

Page 48: Objects, Objects Everywhere

Ruby

Literal hashes are new objects{}.object_id #=> 7019206373870

{}.object_id #=> 70192063701240

Page 49: Objects, Objects Everywhere

Ruby

Literal regexs are new objects//.object_id #=> 70192063385520//.object_id #=> 70192067965040

Page 50: Objects, Objects Everywhere

Ruby

Literal ranges are new objects(1..2).object_id #=> 70192067946460(1..2).object_id #=> 70192067921120

Page 51: Objects, Objects Everywhere

Ruby

Not everything creates a new object

Ruby has singleton objects (objects instantiated once).

Page 52: Objects, Objects Everywhere

Ruby

nil is a singletonnil.object_id #=> 4nil.object_id #=> 4

Page 53: Objects, Objects Everywhere

Ruby

booleans are singletonsfalse.object_id #=> 0false.object_id #=> 0true.object_id #=> 2true.object_id #=> 2

Page 54: Objects, Objects Everywhere

Ruby

Numbers are singletons1.object_id #=> 31.object_id #=> 3

Page 55: Objects, Objects Everywhere

Ruby

Numbers are singletons1.object_id #=> 31.object_id #=> 3

Fixnums

Page 56: Objects, Objects Everywhere

Ruby

Bignums are not singletons

4611686018427387904.object_id #=> 70192063730740

4611686018427387904.object_id #=> 70192063508580

Page 57: Objects, Objects Everywhere

Ruby

4611686018427387904.object_id #=> 70192063730740

4611686018427387904.object_id #=> 70192063508580

Why 4611686018427387904?

Bignums are not singletons

Page 58: Objects, Objects Everywhere

Ruby bytes = 0.size #=> 8

bytes to store integer

Page 59: Objects, Objects Everywhere

Ruby

bits = bytes * 8 #=> 64

8 bits per byte

64 bit machine

bytes = 0.size #=> 8

bytes to store integer

Page 60: Objects, Objects Everywhere

Ruby bytes = 0.size #=> 8

bytes to store integer

bits = bytes * 8 #=> 64

8 bits per byte

2 ** (bits - 2) #=> 4611686018427387904

1 bit for sign + 1 bit for Ruby

64 bit machine

Page 61: Objects, Objects Everywhere

Ruby

4611686018427387904Bignum

4611686018427387904 - 1Fixnum

Different objects.

Singleton object.

Page 62: Objects, Objects Everywhere

Ruby

Why does nil have object_id of 4?

1 have object_id of 3?

true have object_id of 2?

Page 63: Objects, Objects Everywhere

Ruby

Because Matz says sofalse.object_id #=> 0

0.object_id #=> 1true.object_id #=> 2

1.object_id #=> 3nil.object_id #=> 4

2.object_id #=> 5

Page 64: Objects, Objects Everywhere

Ruby

Find by object_idObjectSpace._id2ref(0) #=> falseObjectSpace._id2ref(1) #=> 0ObjectSpace._id2ref(2) #=> trueObjectSpace._id2ref(3) #=> 1ObjectSpace._id2ref(4) #=> nilObjectSpace._id2ref(5) #=> 2ObjectSpace._id2ref(6) #=> errorObjectSpace._id2ref(7) #=> 3ObjectSpace._id2ref(8) #=> error

Page 65: Objects, Objects Everywhere

Ruby

Negative object_ids-1.object_id #=> -1-2.object_id #=> -3-3.object_id #=> -5

Page 66: Objects, Objects Everywhere

Objects.useful? # => true

Page 67: Objects, Objects Everywhere

Objects.useful? # => trueConsistent modeling across the stack.

Page 68: Objects, Objects Everywhere

Objects.useful? # => trueHelpful in representing the real world.

Page 69: Objects, Objects Everywhere

Objects.useful? # => trueFun and expressive.

Page 70: Objects, Objects Everywhere

Thanks!@zombidev