Section 5 - Object Oriented Javascript and Prototypal Inheritance
History, Function Constructors, and 'new'
Javascript was named the way it was because the original developers wanted to attract developers from java.
A form of marketing.
Even though it was nothing like java.
Lets take a look on the few different ways we've learned to construct objects.
Method 1
Method 2
Method 3 (new method)
How the new keyword works
Creates a new empty object (person), whose 'this' contains nothing.
Then the properties are supplied to 'this'
The new object becomes returned
Make a dynamic constructor (preferred way):
Even though js does not have classes, personConstructor can behave similar to one.
Function Constructor and Prototype
Now lets discuss how inheritance works.
prototype is a keyword that is specifically used with function constructors.
Its like extending the definition of an object.
It can be helpful because every unique object will have access to their inherited prototype.
Still I'd recommend ec6 classes over prototypes.
The neat thing about this is that regardless of how many objects we have, we can define a property for all of those objects very quickly.
You may think that it is just as effective to place getFullName() to the function constructor.
You are correct in that this will work, but its not exactly as effective.
We have to remember that the objects we construct exist in memory, and if we had a million objects, each object would have all the properties we've defined it have.
Sigh... thinking back to my earlier C++ projects, I've made so many mistakes without this idea.
We dont want to waste memory space for something that is not-unique.
Afterall, getFullName() references a static element of that object. Why would we want to create the same method for each object?
getFullName() references elements from the object that are unique.
So it would make sense to define the firstname and lastname in the constructor.
This is why in general we define methods in the prototype.
Its like class definitions in C++.
We dont construct a object with function defintions, that would be absurd. Instead, we define methods in the class, that each object has assess from. What we construct the object with are essentially uniquely defined variables that resonate with its class.
There is a convention used with function constructors. Begin the name with a capital letter, as a reminder for yourself to use the new keyword.
Built-in Function Constructors
The js engine also has some neat ways of creating new objects with in-built functions.
And the result creates an object with a lot of neat properties. String, for example, automate this procedure.
We can also explore each functions prototype methods as well.
Now, like before, we can extend our own prototype methods to this inbuilt function, just like any other object.
'this' points to the object String, and prototype extends its definition with a function we've defined for ourselves.
.length is an inbuilt function (shown before)
Another example:
Notice that a is an object. It is not a number, even though it looks like a primitive form.
this directly points to the object in Number.
Side note: It is generally not recommended to use function constructors, because it can get messy. Why? Because we've complicating the whole primative vs nonprimitive thing.
As a side note, arrays act a little different as objects.
The indexs are actually just properties of the array.
That means if we were not modified its Object form, things can get messy.
The properties may not print out as you might expect them to.
Further more, now that we know how the properties are created, it explains why we're able to navigate through the array object like this.
Objects.create and Pure Prototypal Inheritance
Objects.create is yet another way to construct new objects
Object.create constructs a new Object, but it is not the immediate object. Nothing gets overwritten either.
Instead, its creates a NEW empty objects whose prototype is a copy of the object passed in.
The idea here is that we can overwrite whatever properties we want.
Also notice that John.first writes to the immediate empty object. The prototype stays as it was initially.
The key here is that the prototype remains as only a single copy.
This is quite useful because the methods are essentially inherited.
Sometimes, it is necessary to create whats known as a polyfil. Not all browsers support Object.create, as it is a reletively new in modern browsers. Lets write our own Objects.create()
What we know about object.create:
A new empty object is created -> function f() {};
The empty object prototype points to and copies the arguments of the objects passed in.
Inheritance: (personally) a better approach
ES6 and Classes
ES6 is now released an supported as of June 2015.
Introduces classes
Pretty cool, coming from a C++ background
Notice that the browser currently requires declarations used in 'strict mode';
Because ES6 is not wildly supported yet, it is still recommend to stick with ES5 - especially if your using implementations from different js libraries like moments.js, underscore.js, etc.
Because it may take some time to update this libraries to support the new changes.
Classes are OBJECTS TOO!
That means we can use and modify them as objects.
Inheritance with classes:
We've learned many ways of constructing an object.
Syntactic sugar, is a word thrown around when their is a different way to write something, but the way it works under the hood is not changed.
Last updated
Was this helpful?