Jordan Kasper
A way of representing things as collections of code with data and actions, including mechanisms for reusing code among related collections.
"Abstraction is the stripping away of details so that you can see the essence." (Douglas Hofstatder)
Creating an interface for your object model and abstracting the implementation of the actions of that model.
Can you explain the relationship between two objects "A" and "B" by: "A is a B" within the code structure provided in the language?
Literally: "many forms". The ability for a member of a collection to have different forms within the inheritance chain without changing the interface exposed outside the object.
JavaScript is a multi-paradigm language.
FYI, while undefined
is clearly not an object,
null
is: typeof null === "object"
Using the "curly brace" syntax (object literals)...
which is equivalent to...
this
inside an object method refers to the current context.
Context is typically the variable before the "." when called:
this === literalDog
inside speak()
Notice how our name
and speak
members are NOT in our prototype!
JavaScript is actually taking 3 distinct actions:
__proto__
property to point to Dog.prototype
;constructor
method on the new object.In fact, we can do these actions on our own:
Note that Object.create()
does not exist in IE < 9, but you can use a shim.
What about name
and speak
not being on the prototype?
We probably want them to be...
(That's pronounced "dunder proto", as in: "double underscore proto")
__proto__
has existed for a long time, and in ES6/ES2015 it is deprecated
public, private, privileged
Anything attached to the prototype
:
Anything on this
or a reference to the object instance:
And privileged methods
Variables declared inside a function
block only exist while that function's execution context exists.
If we want access to a private variable we have to create a "privileged" method...
Access to the alive
variable from isAlive()
and die()
is possible
because functions in JavaScript are closures.
If we want access to a private variable we have to create a "privileged" method...
Static members are set directly on our Dog constructor function.
Be careful when using static methods,
you cannot access the instance (this
) inside them!
We need to copy our properties and methods
from the mixin to the desired prototype...
Use a helper method!!
This looks nice, but it's just syntactic sugar!
In my opinion, this will just lead to more confusion...
There are plenty of good solutions out there...
Jordan Kasper