Weitere Àhnliche Inhalte
Ăhnlich wie Objects, Objects Everywhere (20)
KĂŒrzlich hochgeladen (20)
Objects, Objects Everywhere
- 3. Letâs Talk Objects
Behavior
Iâm a person, what can I do?
Walk
Talk
Eat
Could also be deïŹned as:
Move Legs
Move Lips
Ingest Food
- 4. Letâs Talk Objects
Behavior
Level of abstraction matters!
Behavioral abstraction varies per domain.
- 5. Letâs Talk Objects
State
Iâm a person, who am I currently?
Mike Pack
Male
Brown
Therefore, I have attributes:
Name
Gender
Hair Color
- 6. Letâs Talk Objects
State
States change
When Iâm 70, my attributes might be:
Mike Pack
Male
Grey
- 7. Letâs Talk Objects
Behavior changes too!
I just learned to jump, now my behavior is:
Walk
Talk
Eat
Jump
- 10. Attribute
HTML Name
<div id=ânameâ>
Mike
</div>
Is this an object?
- 11. Attribute
HTML Name
<div id=ânameâ>
Mike
State
</div>
Is this an object?
- 12. Attribute
HTML Name
<div id=ânameâ>
Mike the beef?
Whereâs State
</div>
Is this an object?
- 13. Attribute
HTML Name
<div id=ânameâ>
Mike behavior?
Whereâs the State
</div>
Is this an object?
- 15. Attribute
HTML Name
<div id=ânameâ
onclick=â...â>
Mike Behavior!
</div> State
Is this an object?
- 16. HTML
HTML is object oriented
(albeit, not for purist)
HTML does not exhibit other properties of an OO systems:
- Inheritance
- Polymorphism
- etc
- 17. HTML
Donât use onclick=â...â
However, without onclick, HTML does not exhibit behavior.
- 19. CSS
Attribute
a{ Name
color: red;
}
Is this an object?
- 20. CSS
Attribute
a{ Name
color: red;
} State
Is this an object?
- 21. CSS
Attribute
a{ Name
color:the beef?
Whereâs red;
} State
Is this an object?
- 22. CSS
a{
color: red;
&:hover {
color: blue;
}
}
Is this an object?
- 23. Attribute CSS
Name a {
color: red; Behavior!
&:hover {
color: blue;
}
State
}
Is this an object?
- 24. CSS
a{
Behavior color: red; State
&:hover { Change
color: blue;
}
}
Behavior changes state.
- 25. CSS
CSS is object oriented
(not for purist)
CSS does not exhibit other properties of an OO systems:
- Inheritance
- Polymorphism
- etc
- 27. Attribute
Name
JavaScript
var book = {
title: âThe Art of
Warâ
};
Is this an object?
- 28. Attribute
Name
JavaScript
var book = {
title: âThe Art of
Warâ State
};
Is this an object?
- 29. Attribute
Name
JavaScript
var book = {
title: âThe Art of
Whereâs the beef?
Warâ State
};
Is this an object?
- 30. Attribute
Name
JavaScript
var book = {
title: âThe Art of
Whereâs the beef?
ItâsWarâ
implicit! State
};
Is this an object?
- 31. JavaScript
var book = {
title: âThe Art of Warâ,
constructor: function() {...},
hasOwnProperty: function() {...},
...
};
Implicit behavior.
- 38. JavaScript
typeof might not return object
typeof true === âobjectâ //=> false
typeof true === âbooleanâ //=> true
But booleans are objects!
new Boolean(1).valueOf() === true //=> true
- 40. Ruby
1
Is this an object?
- 41. Ruby State
(accessor)
1
1.real #=> 1
Is this an object?
- 42. Ruby State
(accessor)
1
1.real #=> 1
1 + 1 #=> 2
Is this an object?
- 43. Ruby State
1 (accessor)
1.real #=> 1
Behavior 1 + 1 #=> 2
1.+(1) #=> 2
Is this an object?
- 47. Ruby
Literal strings are new objects
âslurpyâ.object_id #=> 70318249561400
âslurpyâ.object_id #=> 70318249500340
- 51. Ruby
Not everything creates a new
object
Ruby has singleton objects (objects instantiated once).
- 52. Ruby
nil is a singleton
nil.object_id #=> 4
nil.object_id #=> 4
- 55. Ruby
um s
xn
Fi Numbers are singletons
1.object_id #=> 3
1.object_id #=> 3
- 56. Ruby
Bignums are not singletons
4611686018427387904.object_id #=>
70192063730740
4611686018427387904.object_id #=>
70192063508580
- 57. Ruby
Bignums are not singletons
4611686018427387904.object_id #=>
70192063730740
4611686018427387904.object_id #=>
70192063508580
Why 4611686018427387904?
- 59. bytes to store
Ruby integer
bytes = 0.size #=> 8
8 bits per byte
bits = bytes * 8 #=> 64
64 bit machine
- 60. bytes to store
Ruby integer
bytes = 0.size #=> 8
8 bits per byte
bits = bytes * 8 #=> 64
64 bit machine
1 bit for sign
+ 1 bit for Ruby
2 ** (bits - 2) #=>
4611686018427387904
- 62. Ruby
Why does nil have object_id of 4?
1 have object_id of 3?
true have object_id of 2?
- 63. Ruby
Because Matz says so
false.object_id #=> 0
0.object_id #=> 1
true.object_id #=> 2
1.object_id #=> 3
nil.object_id #=> 4
2.object_id #=> 5
- 64. Ruby
Find by object_id
ObjectSpace._id2ref(0) #=> false
ObjectSpace._id2ref(1) #=> 0
ObjectSpace._id2ref(2) #=> true
ObjectSpace._id2ref(3) #=> 1
ObjectSpace._id2ref(4) #=> nil
ObjectSpace._id2ref(5) #=> 2
ObjectSpace._id2ref(6) #=> error
ObjectSpace._id2ref(7) #=> 3
ObjectSpace._id2ref(8) #=> error