19. OOP:
First Principles function Shirt(owner){
this.owner = owner
}
Shirt.prototype.iron=function(){
this.ironed = true
Constructors }
var myShirt = new Shirt(‘pi’)
Abstraction // myShirt.owner === ‘pi’
myShirt.iron()
Inheritance // myShirt.ironed === true
Encapsulation
Polymorphism
20. OOP:
First Principles
Constructors Shirt
(new)
.iron()
Abstraction .wash()
.ironed // true/false
.age // number
Inheritance
Encapsulation
Polymorphism
21. OOP:
First Principles
Constructors
var Kurta = extends(Shirt)
Abstraction
Kurta.prototype.wash = function(){
Shirt.prototype.wash.apply(this,
Inheritance arguments);
// and make sure the water’s
cold!
Encapsulation }
Polymorphism
22. var __hasProp = {}.hasOwnProperty
OOP:
var extends = function(child, parent) {
for (var key in parent) {
First Principles
if (__hasProp.call(parent, key)) {
child[key] = parent[key]
}
}
function Ctor() {
this.constructor = child
}
Ctor.prototype = parent.prototype
child.prototype = new Ctor()
Constructors child.superclass = parent.prototype
return child
}
Abstraction
var Kurta = extends(Shirt)
Inheritance Kurta.prototype.wash = function(){
Shirt.prototype
}
Encapsulation
Polymorphism
23. OOP:
First Principles
Constructors
Abstraction var ctr = (function(){
var i = 0
return function(){
Inheritance return i++
}
})()
Encapsulation
ctr() // 0
ctr() // 1
Polymorphism ctr() // 2
// and so on
24. OOP:
First Principles
Constructors
Abstraction
Inheritance
// make do with runtime object
Encapsulation // manipulation, “arguments”
myShirt.iron = function(x, y){
Polymorphism x === arguments[0] // true
y === arguments[1] // true
var z = arguments[3]
// doop de doo
}