-
Notifications
You must be signed in to change notification settings - Fork 2
Pseudoclassical Class Definition OOP
Pseudoclassical class definition happens in 2 code blocks instead of one, which happens in other languages like Python and PHP.
The first block is called the "Constructor Function" which is where attributes of the class is declared. These are the aspects of the class that are unique to each new instance. Example with cars is that brand, color, and location can differ. In the second block of code you declare the methods that will be shared by each instance of the class. Examples are things the car can do, drive forward, stop, open door.
var Car = function(brand, color, location) {
this.brand = brand;
this.color = color;
this.location = location
};
Car.prototype = {
move: function() { this.location++; },
stop: function() { this.location = 0; },
};
The reason for declaring the entire class in 2 blocks is to save on memory when you start creating instances of the class. If the class declaration was "Functional" style then there is a new copy of the method(s) made for each instance. By declaring the class "Pseudoclassical" style only a single of copy of the methods are stored in memory.
When a instance of the class attempt to access a method:
var x_car = new Car('lexus', 'white', 0);
x_car.move();
The interpreter will actually first fail to find the called method in the object itself since it was made from the Car constructor function. As you can see above there is no reference to any of the methods in the Car constructor function. From there the interpreter searches to the Car.prototype
which is now shared between all instances. There the interpreter find the method that was called!
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links