-
Notifications
You must be signed in to change notification settings - Fork 71
/
9.1-Pseudoclassical Subclasses.js
72 lines (53 loc) · 2.76 KB
/
9.1-Pseudoclassical Subclasses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// How to write a Pseudoclassical Subclass?
// **** library.js ****
var Car = function(loc) {
this.loc = loc;
};
// now add move function
Car.prototype.move = function () { // add move as a method to the Car.prototype constructor (pseudoclassical pattern)
this.loc++;
};
// Write constructor function for Van - want to run the Car fcn in the middle of Van in a way that modifies the Van instance that is being created
var Van = function(loc) {
// this = Object.create(Van.prototype); -> invisible code that comes with 'new'
// incorrect approaches under here:
// this.loc = loc; -> repeating the same code from the Car fcn is not efficient
// new Car(loc); -> this creates another new instance object of Car, the 'this' within Car will point to a new instance of Car, not to Van
Car.call(this, loc); // .call method of any function ensures that we run the function in the right context
// -^- calls Car in the same context as the new Van instance is being created (in parameter 'this').
// 'this' binds Car to the right object in Van, just like any positional parameter.
};
// Illustrative example using a math function
var product = function(num, b){
return num * b;
};
var double = function(num){
return product(num, 2); // num is like 'this' in above example; two 'num' vars are in different scopes
};
double(3);
// now use .call method
var product = function(b){
return this * b;
};
var double = function(){
return product.call(this, 2); // invokes the function a little differently; binds the value to parameter this
};
double.call(3); // 3 is bound to the parameter this
// *** Delegating Van.prototype *** (3 lines)
// by default, Van.prototype delegates to Object.prototype. Need to wire up Van.prototype to Car.prototype
Van.prototype = Object.create(Car.prototype); // this creates a new object that delegates to what is in the ()'s, without running the constructor function in the process
// careful not to mix-up 'Car' with Car.prototype!
// Van.prototype = new Car(); // common mistake. Problematic bc whenever make a Car subclass (like Van), we invoke the function Car, and don't pass in the right variables
// Van.prototype = Car.prototype; // JS does not allow copying like this, this would modify Car when you modify Van
// Van.prototype.constructor = Car; // Van's subclasses delegate to Car by default - need to reset Van's constructor
Van.prototype.constructor = Van; // now the lookup for amy falls through to Van.prototype
// Add grab to Van
Van.prototype.grab = function(){};
// ***** run.js *****
var zed = new Car(3); // instantiate one instance of superclass
zed.move();
// Van in a subclass of Car
var amy = new Van(9); // instantiate one instance of one subclass - amy is instance of Van
console.log(amy.loc); // -> 9
// amy.move();
// amy.grab();