Created
April 15, 2016 21:58
-
-
Save tebba-von-mathenstein/88e7a4c0a1403a7fb296bb7eaf0b9804 to your computer and use it in GitHub Desktop.
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
// Function definition. This creats a function called reverseString | |
// which accepts one parameter. | |
function reverseString(inputString) { | |
var outputString = ""; | |
for(var i = inputString.length - 1; i >= 0; i--) { | |
var currentChar = inputString[i]; | |
outputString += currentChar; | |
} | |
return outputString; | |
} | |
// You can call functions from within functions. | |
function isThisAPalindome(inputString) { | |
var reversed = reverseString(inputString); | |
return inputString === reversed; | |
} | |
// Lets test our function | |
console.log(isThisAPalindome("hello")); // false | |
console.log(isThisAPalindome("bob")); // true | |
console.log(isThisAPalindome("boB")); // false | |
console.log(isThisAPalindome("racecar")); //true | |
// A function without a return statement returns undefined | |
function returnsNothing() { | |
var x = 50; | |
x; | |
} | |
var x = returnsNothing(); | |
console.log(x); | |
// Store a function in a variable. | |
// This is what makes people say JavaScript has "first class functions." | |
var yFunction = returnsNothing; | |
console.log(yFunction); // Prints that yFunction is a "function" | |
console.log(yFunction()); // Calls yFunction, which is returnsNothing, prints undefined | |
// addN returns an "anonymous function" | |
function addN(n){ | |
// This anonymous function contains a "closure". | |
// The variable n is "closed over" or saved in the function that gets returned | |
return function(x) { | |
return x + n; | |
} | |
} | |
var addSeven = addN(7); // Here, addSeven is the anonymous function but "closes over" n where n=7 | |
var addNine = addN(9); // Here, addNine is the anonymous function but "closes over" n where n=9 | |
console.log(addSeven); // Function | |
console.log(addNine); // Function | |
console.log(addSeven(8)); // 15 | |
console.log(addNine(1)); // 10 | |
// This function takes a function as a parameter, then calls the passed in function. | |
// When a function is passed as a parameter, it is often refered to as a "callback" | |
function callThisCallback(callbackFunction) { | |
return callbackFunction(); | |
} | |
// This function takes a callback, and a parameter to pass to that callback. | |
// Above we just call the function with nothing | |
function callbackWithParam(callbackFunction, inputToCallback){ | |
return callbackFunction(inputToCallback); | |
} | |
// These two lines do the same thing. | |
// Both ultimately call the function "addSeven" without passing a parameter. | |
// They both compute 7 + undefined, which is NaN (Not a Number) | |
console.log(callThisCallback(addSeven)); | |
console.log(addSeven()); | |
// These both also do the same thing, call addSeven but with a parameter of 7 | |
// meaning they do 7+7 = 14 | |
console.log(callbackWithParam(addSeven, 7)); | |
console.log(addSeven(7)); | |
// Finally, this is called the "self envoking function syntax" | |
// An anonymous function is created, then immediately envoked | |
// In this case we are passing "one" as parameter, but because the function | |
// expects two parameters this self envoking function prints | |
// one, undefined | |
(function(parameter, two) { | |
console.log(parameter) // "one" | |
console.log(two) // undefined | |
})("one"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment