Last active
December 29, 2015 01:08
-
-
Save awhit012/403fde5374f5cd37f07e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class notes: https://github.com/DelmerGA/intermediate_talk | |
// javascript in one pic: https://github.com/coodict/javascript-in-one-pic | |
// Our code to do something a random number of seconds later | |
var fullName = function(args){ | |
return args.firstName + " " + args.lastName; | |
}; | |
var joke = function(args){ | |
return "I'm against picketing but I don't know how to show it." + " " + args.crowdReaction | |
} | |
var random = function(low, high){ | |
var low = low * 1000 | |
var high = high * 1000 | |
return Math.floor(Math.random() * (high - low + 1)) + low; | |
}; | |
var tellMeLater = function(callback, args){ | |
args = args || {} | |
setTimeout( function(){ console.log( callback(args))}, random(2,5)); | |
}; | |
var joke_params = {crowdReaction: "BOOOOO!!!!!"} | |
tellMeLater(joke, joke_params); | |
var person = {firstName: "Michael", lastName: "Jackson"} | |
tellMeLater(fullName, person); | |
// Basic Constructor Function | |
function Person (first, last) { | |
// set firstName and lastName | |
this.firstName = first; | |
this.lastName = last; | |
} | |
var myPerson = new Person("Michael", "Jackson"); | |
console.log(myPerson.firstName) | |
// Basic Prototype Usage | |
function SuperHero(name, alterEgo){ | |
this.name = name; | |
this.alterEgo = alterEgo; | |
} | |
SuperHero.prototype.superPunch = function(){ | |
console.log("WHAMMM!!!!") | |
} | |
var myHero = new SuperHero("Robo Dude", "Alex") | |
// console.log(myHero.alterEgo); | |
// myHero.superPunch(); | |
function Robot(name, purpose){ | |
this.name = name; | |
this.purpose = purpose; | |
} | |
Robot.prototype.greet = function(){ | |
console.log("Beep boop"); | |
}; | |
niceRobot = new Robot("Wallee", "collect garbage") | |
myRobot = new Robot("HAL", "Protect the mission"); | |
myRobot.purpose = "Kill everyone" | |
console.log(myRobot.purpose) | |
console.log(niceRobot.purpose) | |
// Basic Inheritence Example | |
function Animal(){ | |
this.alive = true; | |
}; | |
function Mammal(){ | |
this.blood = "warm" | |
}; | |
function Cat(){ | |
}; | |
function Reptile(){ | |
this.blood = "cold" | |
}; | |
function KommodoDragon(){ | |
}; | |
Cat.prototype = new Mammal(); | |
Reptile.prototype = new Animal(); | |
KommodoDragon.prototype = new Reptile(); | |
grimm = new Cat(); | |
puff = new KommodoDragon(); | |
console.log(grimm.blood); | |
console.log(puff.alive); | |
console.log(Cat.prototype.constructor ); | |
// testing using Mocha | |
// the assert library built into node | |
var assert = require("assert"); | |
/* | |
describe doesn't really do much, | |
beyond running the code in the | |
function it's given | |
*/ | |
/* | |
note we are using describe to | |
group a set of tests all related | |
to the Arrays | |
*/ | |
describe('Array', function(){ | |
/* | |
here we use a describe to | |
indicate another group of | |
tests, but yet again it isn't | |
doing anything really | |
*/ | |
/* | |
we are trying to describe a | |
group of tests related to | |
the Array#indexOf method | |
*/ | |
describe('#indexOf()', function(){ | |
/* | |
the it method does all the work | |
in mocha, it takes a message to | |
print if the test fails for a | |
behavior | |
*/ | |
/* | |
here we are trying to that | |
Array.indexof behaves well | |
when values are not in the | |
array. | |
*/ | |
it('should return -1 when the value is not present', function(){ | |
// We are using assert to throw errors if something is wrong | |
assert.equal(-1, [1,2,3].indexOf(5)); | |
assert.equal(-1, [1,2,3].indexOf(0)); | |
}) | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment