January 2017
Beginner to intermediate
550 pages
10h 6m
English
Lets try and solve the following exercise:
shape that has a type property and a getType() method: var shape = {
type: 'shape',
getType: function () {
return this.type;
}
};
Triangle () constructor: function Triangle(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
Triangle.prototype = shape;
Triangle.prototype.constructor = Triangle;
Triangle.prototype.type = 'triangle';
getPerimeter() method, use the following code: Triangle.prototype.getPerimeter = function () {
return this.a + this.b + this.c;
};
> var t = new Triangle(1, 2, 3); > t.constructor === Triangle; true > shape.isPrototypeOf(t); true > t.getPerimeter(); 6 > t.getType(); ...
Read now
Unlock full access