Object Oriented Programming Sunil Pai, Y!
Objects
Objects and Javascript
Numbers Strings Booleans Objects and Javascript Regexps Functions Arrays Objects
3.141 Numbers 1.4e20 0xff3322 Strings Booleans Objects and Javascript Regexps Functions Arrays Objects
Numbers “the lazy dog” Strings “123” “1” + 1 Booleans Objects and Javascript Regexps Functions Arrays Objects
Numbers Strings true Booleans false Objects and Javascript Regexps Functions Arrays Objects
Numbers Strings Booleans Objects and Javascript Regexps /[A-Za-z0-9]/gm Functions Arrays Objects
Numbers Strings Booleans Objects and Javascript Regexps function(p1, p2){ // do something Functions // with p1, p2 return p1 + p2 } Arrays Objects
Numbers Strings Booleans Objects and Javascript Regexps Functions Arrays [ 1, ‘abc’, 2.4 ] Objects
Numbers Strings Booleans Objects and Javascript Regexps Functions Arrays { x: 200, Objects y: 164, theta: 20 }
null Objects and Javascript undefined
Javascript has superpowers
functions are objects Javascript has closures superpowers .prototype
functions are objects Javascript has closures superpowers .prototype
functions are objects Javascript has closures superpowers .prototype
functions are objects Javascript has closures superpowers .prototype
OOP: First Principles Constructors Abstraction Inheritance Encapsulation Polymorphism
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() // myShirt.ironed === true Inheritance Encapsulation Polymorphism
OOP: First Principles Constructors Shirt (new) .iron() Abstraction .wash() .ironed // true/false .age // number Inheritance Encapsulation Polymorphism
OOP: First Principles Constructors Abstraction var Kurta = extends(Shirt) Inheritance Kurta.prototype.wash = function(){ Shirt.prototype } Encapsulation Polymorphism
OOP: var __hasProp = {}.hasOwnProperty 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
OOP: First Principles Constructors Abstraction var ctr = (function(){ var i = 0 Inheritance return function(){ return i++ } })() Encapsulation ctr() // 0 ctr() // 1 Polymorphism ctr() // 2 // and so on
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 }
( Demo )
sidenote: MVC
sidenote: modules
sidenote: frameworks
sidenote: other common patterns
Questions?
Party on, dudes. @threepointone
Recommend
More recommend