OBJECT-ORIENTED JAVASCRIPT
*Ok - maybe it does just in ES6
JavaScript is an object-oriented programming language, but it does not have a "formal" way of creating class constructors* like most languages, hence the name: pseudoclasses.
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function Building(floors) {
this.what = "building";
this.floors = floors;
}
Constructor Properties (per instance)
Methods (for all instances)
Building.prototype.countFloors = function() {
console.log('I have', this.floors, 'floors');
};
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function Building(floors) {
this.what = "building";
this.floors = floors;
}
Building.prototype.countFloors =
function() {
console.log('I have',
this.floors, 'floors');
};
var yourHouse = new Building(2);
var theOffice = new Building(12);
yourHouse.countFloors();
theOffice.countFloors();
The results of the shared method depend on the unique instance values which are created at call-time inside each function's scope.
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
Create a unique constructor that makes a building of your choice using the pseudoclassical pattern.
Don't forget to add methods and practice creating instances.
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
0
1
2
3
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
var makeEggs = function(style, n) {
var completedEgg;
if (style !== "boiled") {
var crackedEggs = crackEggs(n);
if (style !== "scrambled") {
completedEgg = fryEgg(crackedEggs, style);
} else {
var preppedEggs = whipEggs(crackedEggs)
completedEgg = fryEgg(preppedEggs)
}
}
//... other procedures here
return completedEgg;
}
makeEggs('scrambled', 3);
makeBacon('crispy', 2)
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
var Stack = function(){
this.storage = "";
};
Stack.prototype.push = function(val){
};
Stack.prototype.pop = function(){
};
Stack.prototype.size = function(){
};
var myWeeklyMenu = new Stack();
myWeeklyMenu.push("RedBeans");
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
(aka enqueued)
(aka dequeued)
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
Recursion is simply when a function calls itself; however it doesn't stop there.
PRESENTED BY BIANCA GANDOLFO
var callMe = function() {
callMe();
callMe();
callMe('anytime');
};
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
Recursive Functions
Recursive Algorithms
Recursive Data Structures
var callMe = function() {
callMe();
callMe();
callMe("anytime");
};
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
var callMe = function() {
callMe();
callMe();
callMe("anytime");
};
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
var tracker = 0;
var callMe = function() {
tracker++
if (tracker === 3) {
return 'loops!';
}
return callMe('anytime');
};
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
var callMyself = function() {
if() {
// base case
return;
} else {
// recursive case
callMyself();
}
return;
};
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
var loopNTimes = function(n) {
console.log('n equals', n)
if (n <= 1) {
return 'complete';
}
return loopNTimes(n-1);
};
loopNTimes(3);
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function computeFactorial(num) {
var result = 1;
for(var i = 2; i <= num; i++) {
result *= i;
}
return result;
}
results *= 2;
results *= 3;
results *= 4;
results *= 5;
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function computeFactorial(num) {
if(num === 1) {
return 1;
} else {
return num * computeFactorial(num - 1);
}
}
5 * computeFactorial(4)
4 * computeFactorial(3)
3 * computeFactorial(2)
2 * computeFactorial(1)
1
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function logNumbers(start, end) {
for(var i = start; i <= end; i++) {
console.log(i);
}
}
function logNumbersRecursively(start, end) {
function recurse(i) {
console.log(i);
if(i < end) {
recurse(i + 1);
}
}
recurse(start);
}
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function runRecursiveLoop(start, end) {
function recurse(i) {
console.log(i);
if(i < end) {
recurse(i + 1);
}
}
recurse(start);
}
function runLoopAsMyself(i, end) {
console.log(i);
if(i < end) {
runLoopAsMyself(i + 1, end);
}
}
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO
function joinElements(array, joinString) {
function recurse(index, resultSoFar) {
resultSoFar += array[index];
if(index === array.length - 1) {
return resultSoFar;
} else {
return recurse(index + 1, resultSoFar + joinString);
}
}
return iterate(0, '');
}
OBJECT-ORIENTED JAVASCRIPT
PRESENTED BY BIANCA GANDOLFO