Last active
August 29, 2015 14:05
-
-
Save swaters86/a5b3ba4c2580a46c4a90 to your computer and use it in GitHub Desktop.
Learning JavaScript
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
// Most of this was taken from this awesome blog post: https://www.discovermeteor.com/blog/javascript-for-meteor/?utm_content=bufferf9e86&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer | |
// If you happen to the be the author of this blog post and you don't like how I just scaped it and put all of this code in this Gist then please feel free to email me at swaters86@gmail.com and let me know that I should take it down. | |
// Other sources: | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript | |
// http://addyosmani.com/resources/essentialjsdesignpatterns/book/#prototypepatternjavascript | |
// How to declare a variable | |
var a; | |
// How to declare a LOCAL variable and assign a value to it | |
// Will restrict it's scopt to the function you're declaring it in | |
var a = 12; | |
// How to declare a GLOBAL variable and assign a value to it | |
// Will make it avaiable to your whole script, try to avoid this so you don't pollute the global scope | |
a = 12; | |
// How to declare a function in JavaScript | |
var myAwesomeFunction = function ( myArgument ) { | |
myAwesomeFunction( something ); | |
} | |
// Using functions as arguments for other functions | |
// Declare the function to be used as an argument | |
square = function ( a ) { | |
return a * a; | |
} | |
// Declare the applyOperation function | |
applyOperation = function ( f, a ) { | |
return f( a ); | |
} | |
// Alert the return value of the 'applyOperation' function when the 'square' function is used as an argument | |
applyOperation(square, 10) // returns 100 | |
// The return statment returns the result of the function , anything after the return statement will not get executed | |
myFunction = function ( a ) { | |
return a * 3; | |
explodeComputer(); // will never get executed (hopefully!) | |
} | |
// If statement in JavaScript | |
if ( foo ) { | |
return bar; | |
} | |
// Shorthand verison of the If statment. This Works as long as the block of code inside the if statment can fit on 1 line. | |
if ( foo ) return bar; | |
// If / Else statemenst in JavaScript | |
if ( foo ) { | |
function1(); | |
} else { | |
function2(); | |
} | |
// If / Else statments have their own shorthand syntax | |
foo ? function1() : function2(); | |
// This is particularly useful when assinging a value to a variable | |
var n = foo ? 1 : 2; | |
// If / Else statment with another If / Else statment | |
if ( foo ) { | |
function1(); | |
} else if ( bar ) { | |
function2(); | |
} else { | |
function3(); | |
} | |
// How to define an array in JavaScript | |
var a = [ 123, 456, 789 ]; | |
// How to access an items in the above array | |
a[1]; // 456 | |
// How to define an object in JavaScript | |
myProfile = { | |
name: "John Smith", | |
email: "johnsmith@gmail.com", | |
'zip code': 12345, | |
isInvited: true | |
} | |
// You can nest objects and even use arrays in JavaScript objects | |
myProfile = { | |
name: "John Smith", | |
email: "johnsmith@gmail.com", | |
city: "San Francisco", | |
points: 1234, | |
isInvited: true, | |
friends: [{ | |
name: "John Doe", | |
email: "johndoe@gmail.com" | |
}, { | |
name: "Jane Doe", | |
email: "janedoe@gmail.com" | |
}] | |
} | |
// Accessing an object's property (using dot notation with array syntax) | |
myProfile.name; // John Smith | |
myProfile.friends[1].name; // Jane Doe | |
// Instead of defining 'square' function and passing it as an argument, we are defining it inside of the argumetn call | |
applyOperation = function ( f, a ) { | |
return f( a ); | |
} | |
applyOperation( | |
// Defining 'square' function | |
function ( a ) { | |
return a * a; | |
}, | |
10) // 100 | |
// Pushing new items to an array | |
var myArray = [ 123, 456 ]; | |
myArray.push( 789 ) // 123, 456, 789 | |
// String replacements | |
var myString = "abcdef"; | |
myString.replace("a", "z"); // "zbcdef" | |
// Changing functions in JavaScript using the 'something.function1().function2().function3()' pattern | |
n = 5; | |
n.double().square(); //100 | |
// Assignment operators | |
// Assinging a value to a variable | |
a = "12"; | |
// Comparing a to 12 | |
a == 12; // true | |
// Comparing both value and type | |
// a is a string value , while 12 is not an integer so false is returned | |
a === 12; // false | |
// Inequality Operator | |
a = 12; | |
a !== 11; // true | |
// Getting the opposite of a boolean value using the ! inequality operator | |
a = true; | |
!a; // false | |
// A boolean value is always returned even if the varaible you're chekcing against isn't a boolean value | |
a = 12; | |
!a; // false | |
// Converting a variable by using the ! inequality operator twice (once to force the variable to boolean, a second time to revert the value back) | |
a = 12; !! a; // true | |
// Or | |
a = 0; !! a; // false | |
// How to set a timeout for executing a function in 5 seconds | |
window.setTimeout(function() { | |
alert( "Hello World!" ); | |
}, 500); | |
// How to cancel a timeout before it executes a function | |
var timeoutID; | |
timeoutID = window.setTimeout(function() { | |
alert( "Hello World!" ); | |
}, 500); | |
window.clearTimeout(timeoutID); | |
// How to create a prototype | |
var myCar = { | |
name: "Ford Escort", | |
drive: function () { | |
console.log( "Weeee. I'm driving!" ); | |
}, | |
panic: function () { | |
console.log( "Wait. How do you stop this thing?" ); | |
} | |
}; | |
// Use Object.create to instantiate a new car | |
var yourCar = Object.create( myCar ); | |
// Now we can see that one is a prototype of the other | |
console.log( yourCar.name ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment