This lesson assumes you are familiar with the usage of JavaScript Objects and Methods and the fact that JavaScript is a hybrid language.
JavaScript is a hybrid of (at least) three styles:
In this lesson we explore how JavaScript implements object-oriented language features.
This is a simple definition, but many programmers either don't know it, don't understand it, or don't agree with it.
An object...
let rectangle = {
height: 10,
width: 8,
area: function() {
return this.height * this.width;
}
}
let a = rectangle.area()
the above code follows OO rules since it only accesses data held inside rectangle
via the magic pointer this
, and returns a new value, not a live reference to internal state
Using the rectangle
object from the previous slide, the following code is not object-oriented...
let p = calculatePerimeter(rectangle);
function calculatePerimeter(rectangle) {
return rectangle.height * 2 + rectangle.width * 2;
}
...since height
and width
are owned by rectangle
, not by the calculatePerimeter
function
Q: How would an OO design calculate the rectangle's perimeter? (Answer on next slide.)
instead, an OO design would add perimeter
as a method, so rectangle.perimiter()
would access properties with this
, perform the calculation, and return the correct value:
rectangle.perimeter = function() {
return this.height * 2 + this.width * 2
}
let p = rectangle.perimeter()
this
" variablenew
__proto__
or Type.prototype
or class
))One way to think about objects:
Objects are things that can be described and can do things, or...
This code
let dog = {color: "brown"}
'color'
whose value is 'brown'
dog
to point to that objectStack | Heap | |
---|---|---|
dog | -> | {color: "brown"} |
let abby = {color: "brown"}
let lula = {color: "brown"}
abby
refers to a new object instance
lula
refers to a different, new object instance with the same value
Stack | Heap | |
---|---|---|
abby | -> | {color: "brown"} |
lula | -> | {color: "brown"} |
let abby = {color: "brown"}
let abby = dog
let lula = dog
lula.color = "gold"
abby.color // now we think that abby is gold too :-(
Instance variables are properties of the object:
if (abby.color === 'brown') {
console.log("Abby is a brown dog.");
}
the DOT operator here says "get me the color
that is attached to abby
"
Instance methods are also properties of the object:
let abby = {color: "brown"};
abby.speak = function() {
console.log("Bark!")
}
abby.speak() // prints "Bark!" to console
The above is fine as far as it goes, but it's not really object-oriented since speak
isn't using any state...
Unfortunately, in JavaScript, any code with a pointer to an object can see -- and modify! -- all properties of that object.
This means that true encapsulation is difficult, since all properties are public, and none are private.
Other languages solve this problem in various ways -- e.g. Java has a private
keyword, and in Ruby all properties are private -- but JavaScript does not have a clean way of doing it... at least not yet.
To work around this deficiency there are several options, but none is ideal:
* create variables and accessors inside your constructor using closure scope
* prefix private properties with _
* use WeakMaps
* use symbols
var circle = {radius: 2};
circle.circumference = function() {
return Math.PI * 2 * this.radius;
}
console.log(circle.circumference()); // 12.566370614359172
var biggerCircle = {radius: 4};
biggerCircle.circumference = circle.circumference;
console.log(biggerCircle.circumference()); // 25.132741228718345
this
Even inside an object, you must remember to use this.
, otherwise radius
becomes a global variable, not a property.
var circle = {radius: 2};
circle.circumference = function() {
return Math.PI * 2 * radius; // Oops! Forgot "this."
}
circle.circumference() // returns NaN
// (or says 'radius is not defined' if you're lucky)
This is a terrible mistake in the language design; it undercuts one of the core principles of computer language design, which is to make the simplest way to do something also the easiest way to do that thing.
In most OO languages, the pointer this
is managed automatically. Any time you're executing code inside class A, this
is guaranteed to point to an instance of that class.
In JavaScript, this
needs to be managed more actively.
Specifically, during callbacks this
still points to the other object -- the one that is calling you back -- not the object where the function is defined!
One Solution: the =>
fat arrow re-binds this
to point to the current object immediately before executing the function.
this
is only set when you call a function via an objectcircle1.circumference() // OK -- this = circle1
circle2['circumference']() // OK -- this = circle2
this
points to the global object (usually window
)var g = circle.circumference;
g(); // BAD -- this = window, so this.radius = undefined, so result is NaN
var module = {
x: 42,
getX: function() {
return this.x;
}
}
var unboundGetX = module.getX;
console.log(unboundGetX());
// expected output: undefined
var boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
/