Last active
June 6, 2016 17:49
-
-
Save b0uma/a527785079f8ea7414b3 to your computer and use it in GitHub Desktop.
Javascript functions and objects
This file contains hidden or 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
// follow along in the interpreter: node or browser developer console | |
// variable whose value is anonymous function | |
var square = function(num) { | |
return num * num; | |
} | |
// named function | |
function cube(num) { | |
return square(num) * num | |
} | |
// local vs global scope | |
// from eloquent javascript | |
var x = "outside"; | |
var f1 = function() { | |
var x = "inside"; | |
}; | |
f1(); | |
console.log(x); | |
var f2 = function() { | |
x = "inside"; | |
}; | |
f2(); | |
console.log(x); | |
// functions within functions | |
// from eloquent javascript | |
function noisy(f) { | |
return function(arg) { | |
console.log("arg: ", arg); | |
var val = f(arg); | |
console.log("val: ", val); | |
return val; | |
}; | |
} | |
// better noisy with apply | |
function betterNoisy(f) { | |
return function() { | |
console.log("args: ", arguments); | |
var val = f.apply(null, arguments); | |
console.log("val: ", val); | |
return val; | |
}; | |
} | |
// "optional" arguments | |
// from eloquent javascript | |
function power(base, exponent) { | |
if (exponent == undefined) | |
exponent = 2; | |
var result = 1; | |
for (var count = 0; count < exponent; count++) | |
result *= base; | |
return result; | |
} | |
// simple closure | |
// from eloquent javascript | |
function myClosure(n) { | |
var local = n; | |
return function() { return local; }; | |
} | |
// password authentication using a closure | |
// this is a closure because password is a local variable in the passwordChecker function | |
// yet that variable is accessible in the anonymous inner function | |
// returns a function which checks if the guess is equal to password | |
function passwordChecker(password) { | |
return function(guess) { | |
if (guess == password) { | |
console.log('successfully authenticated'); | |
} else { | |
console.log('failure.'); | |
} | |
} | |
} | |
var authenticator = passwordChecker('cool'); | |
authenticator('wrong'); // failure. | |
authenticator('cool'); // successfully authenticated | |
//----------------------------objects in JS------------------------- | |
// constructor to use with new | |
// when this function is called, an object is created | |
// with a prototype that can be accessed with Teacher.prototype | |
var Teacher = function(name, lecture) { | |
this.name = name; | |
this.lecture = lecture; | |
} | |
// don't forget the new keyword | |
var sherif = new Teacher('sherif', function() { | |
console.log('what do you want to cover today? how about ajax?'); | |
}); | |
// because the new keyword was used, julian is associated with the Teacher prototype | |
// therefore, julian can use any methods and properties associated with Teacher.prototype | |
Teacher.prototype.grade = function() { | |
console.log('you get an A'); | |
} | |
// one way to write a constructor | |
// you don't get OOJS prototype functionality | |
var Mentor = function(name, lecture) { | |
mentor = {}; | |
mentor.name = name; | |
mentor.lecture = lecture; | |
return mentor; | |
} | |
var amadou = Mentor('amadou', function() { console.log('enthusiastic encouragement'); }); | |
Mentor.prototype.help = function() { console.log('read the error'); } // should apply to all mentors?? | |
amadou.lecture() // works | |
amadou.help() // error | |
// new won't solve our problems here with the help function | |
var christianne = new Mentor('christianne', function() { console.log('jQuery is great'); }) | |
christianne // { name: 'christianne', lecture: [Function] } | |
christianne.lecture() // works | |
christianne.help() // still doesn't work TypeError: christianne.help is not a function | |
// phase 3 content, read ahead if you are interested | |
// module using an immediately invoking function expression | |
// same password checking functionality as above | |
var soundcloud; // this is the module | |
(function(exports) { | |
var API_KEY = 'secretkey'; | |
exports.authenticate = function(key) { | |
if (key === API_KEY) { | |
console.log("authentication successful"); | |
} | |
else { | |
console.log("go away spammer"); | |
} | |
} | |
})(soundcloud = {}); | |
soundcloud.authenticate('fake'); // go away spammer | |
soundcloud.authenticate('secretkey'); // authentication successful | |
soundcloud.API_KEY = 'evilkey'; // doesn't alter the API_KEY within the module. it's a new property that doesn't matter | |
soundcloud.authenticate('evilkey'); // does not work | |
// augmentation i.e. adding new properties to an existing "module" | |
(function(exports) { | |
exports.playMusic = function() { | |
console.log("boomshakalaka"); | |
}; | |
return exports; | |
}(soundcloud || {})); | |
soundcloud.playMusic(); | |
// sub-modules i.e. module within a module | |
soundcloud.rapModule = (function(exports) { | |
exports = {}; | |
exports.catchPhrase = 'yo yo yo'; | |
return exports; | |
}()); | |
soundcloud.rapModule.catchPhrase; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment