Constructor Functions
Constructor Functions
Defining a constructor function Example of an object using object initialiation
The above literal is a "Book" object type.
Object Type
is defined by it's attributes and behaviors.Behaviors
are represented by methods.
Constructor Functions
: Handle the creation of an object - it's a factory for creating objects of a specific type.There are a few specific things to constructors worth noting:
The name of the constructor function is capitalized
The Function does not explicityly return a value
Within the body, the this keyword references the newly created object
Invoking a constructor function
We can invoke a constructor function using the
new
keyword.
Four Things will happen when invoking a constructor function
A new empty object is created {};
The new obj's
prototype
is set to the object referenced by the constructors prototype property.This
is bound to the new object.The new object is returned after the constructor function has completed.
Understanding New Object Instances
Instance
: term to describe an objected created from a constructor function.Every instance created is a unique object and therefore not equal to each other.
Using the instanceof operator to check an object's type
By using the
instanceof
operator we can verify that an object was created from a certain object type.The instanceOf operator works by checking to see if the prototype object of the left side of the operator is the same as the prototype object of the right side of the operator.
Invoking a constructor function without the new keyword
If we invoke a constructor function without the
new
keyword, we may result in one of two unexpected outcomes:In non-strict mode, this will be bound to the global object instead.
In
strict
mode, this will become undefined.You can enable strict mode by typing
"use strict"
at the top of your file.
Defining Sharable Methods
Avoid the temptation to store an object method inside a constructor function, it is inefficient with computer memory usage b/c each object instance would have it's own method definition.
Prototype
: An object that is delegated to when a reference to an object property or method can't be resolved.Every instance created by a constructor function shares the same prototype.
Object.setPrototypeOf()
andObject.getPrototypeOf()
are just used to set a prototype of one object to another object; and also the verify a prototype.proto
: aka "dunder proto" is a property used to gain easy access to an object's prototype - it is widely supported by browsers but is considered deprecated.
Every method we define on a constructor function's prototype property will be shared across all instances of that object type.
The Problem with Arrow Functions
We cannot use arrow functions when defining methods on a constructor function's prototype property.
Arrow functions don't include their own this binding; therefore it will not reference the current instance - always stick with the function () keyword.
Putting the Class in Javascript Classes
In ES2015, JS gained the class
keyword - replacing the need to use only constructor functions & prototypes to mimic classes!
class
: keyword that gives developers a formal way to create a class definition to specify an object type's attributes and behavior; also used to create objects of that specific type.
Defining a ES2015 class
Class names also begin only with capital letters.
Although not required, class definitions can include a
class constructor function
- these are similar to regular constructors in that:They don't explicitly return a value.
The this keyword references the newly created object instance.
Instantiating an instance of a class
We can also use the
new
.Four things occur when instantiating an instance of a class:
New empty object is created {};
The new obj's prototype is set to the class prototype's property value.
This
is bound to the new object.After the constructor method has completed, the new obj is returned.
Don't try to instatiate a class object without the new keyword.
Class Definitions are NOT hoisted
In JS you can call a function before it's declared - this is known as
hoisting
.Class defs are NOT hoisted, so just get in the habit of declaring them before you use them.
Defining Methods
A class can contain two types of methods:
Instance Method
: Methods that are invoked on an instance of the class - useful for performing an action on a specific instance.Instance methods are also sometimes referred to as
prototype
methods because they are defined on a shared prototype object.
Static Method
: Methods that invoked directly on a class, not on an instance.Important
: Invoking a static method on an instance will result in a runtime error.Prepending the
static
keyword at the beginning on the method name will make it static.
If we go back to an example of how constructor functions also use static methods - we see that static methods are defined directly on the constructor function - whereas instance methods need to be defined on the prototype object.
Comparing Classes to Constructor Functions
ES2015 Classes are essentially syntactic sugar over traditional constructor functions and prototypes.
Javascript Inheritance
Child Class
: Class that is based upon another class and inherits properties and methods from that other class.Parent Class
: Class that is being inherited downwards.Inheritance
: The process of basing a class upon another class.
A
prototype chain
defines a series of prototype objects that are delegated to one by one, when a property or method can't be found on an instance object.When the
getInformation()
method is invoked:JS looks for get() on the current object.
If it isn't found, the method call is delegated to the object's prototype.
It continues up the prototype chain until the method is found.
Overriding a method in a parent class
Method Overriding
: when a child class provides an implementation of a method that's already defined in a parent class.
We can simply declare our own method of the same name in our child class to override our parent's version of
getInformation()
Javascript Modules
Introducing Node.js modules
In Node.js, each JS file in a project defines a
module
.Module's contents are private by default.
Local Modules
: Modules defined within your project.Core Modules
: Native modules contained within Node.js that you can use to perform tasks or to add functionality to your application.CommonJS
: A legacy module system.ES Modules
: Newer module sysem that will eventually replace CommonJS.Entry Point
: JS File that is passed to Node for access to the entire application.Syntax for exporting modules:
Syntax for importing modules:
Using Single Item Modules
Following the convention of a single exported item per module helps to keep modules focused and less likely to become bloted with too much code.
Understanding Module Loading
When loading a module, Node will examine the identifier passed to the require() function to determine if our module is local, core, or third-party:
Local Module
: identifier starts with ./ ../ or /Node.js Core
: identifier matches nameThird-Party
: identifier matches a module in the node modules folder (installed package)
Last updated