Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
lizlongnc / gist:6055aa487a30de94e98a
Last active August 29, 2015 14:07
JS function statement vs function expression
function expression
function
optional name
parameters
wrapped in ()
zero or more names
separated by a comma
body
wrapped in {}
zero or more statements
@lizlongnc
lizlongnc / gist:58661316a11f3d661107
Created October 9, 2014 15:09
JS hoisting in functions
hoisting in functions concern variables
The var statement gets spit into two parts:
The declaration part gets hoisted to the top of the function, initializing with undefined.
The initialization part turns into an ordinary assignment.
var myVar = 0, myOtherVar;
Expands into
@lizlongnc
lizlongnc / gist:e1be4c3dd0ad9803814d
Created October 9, 2014 16:24
JS Module and Singleton
JavaScript is a protypal language but has syntax that is more similar to a classical language which can be confusing.
Function as module (using self-executing function ) is a way to do functions better in some instances.
Function as module safeguards against values being exposed b/c it limits scope:
(function () {
var ...
function ...
function ...
}());
@lizlongnc
lizlongnc / gist:24fb0039b8be5af2352e
Created October 14, 2014 22:27
JS map method on an array object
var people = ['Jim', 'Bob', 'Jane', 'Jill'];
var welcomes = people.map(function(name) {
return 'Hi' + ' ' + name + '!';
});
@lizlongnc
lizlongnc / gist:a802d573e28d7250360f
Last active August 29, 2015 14:07
JS forEach() - arr.forEach(callback[, thisArg])
// arr.forEach(callback[, thisArg])
// REMEMBER!! The forEach uses a function
// The forEach() method executes a provided function once per array element.
// The function can be written outside of the forEach() and called in the forEach() or
// the function can be written inside of the forEach.
// You cannot break from a forEach().
// Each of the 3 code blocks below uses the same function written inside of the forEach().
// also these functions were between script tags in the .html file, if in ext file, they would need to be slightly modified using the this keyword
// For performance-critical scenarios (“hot paths,” tight loops, etc.), a classic for loop is better because the Javascript interpreter can better optimize it. This is also necessary if you need to break from the loop, which you cannot do with forEach.
// http://www.kraigbrockschmidt.com/2013/01/22/array-enumeration-javascript/
from: http://stackoverflow.com/questions/9329446/how-to-do-for-each-over-an-array-in-javascript
For Actual Arrays
(See "For Array-Like Objects" below for array-like objects.)
You currently have three options and will soon have two more:
Use forEach and related (ES5+)
Use a simple for loop
@lizlongnc
lizlongnc / gist:15fea0befc3895794a37
Last active August 29, 2015 14:07
JS for(in) - use with JS objects
for(in) loops through the enumerable properties of an object, not the indexes of an array
console.log('The primary use case for for(in) is to enumerate the properties of an object and will result in unexpected results if used with an Array');
console.log('for(in) gives you the index positions of the items if used in the array and not an objecct:');
console.log('the results below are from using a for(in) with var myNewArray = ["a", "b", "c", "d"]');
var myNewArray = ["a", "b", "c", "d"];
for (var i in myNewArray) {
console.log('i', i);
@lizlongnc
lizlongnc / gist:539e788e6638093da11b
Last active August 29, 2015 14:07
JS FileReader & input type="file"
The input element with a type attribute whose value is "file" represents a list of file items, each consisting of a file name, a file type, and a file body (the contents of the file).
http://www.w3.org/TR/html-markup/input.file.html
http://msdn.microsoft.com/en-us/library/windows/apps/hh466145.aspx
Read Text Files Using the JavaScript FileReader
http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html
READONLY information for an individual file such as its name, size, mimetype, and a reference to the file handle. Adding the "multiple" attribute to the file input element (as in <input type="file" multiple/> will get you a list of files
<input type="file"/>
input always returns a FileList object
@lizlongnc
lizlongnc / gist:90f5d7a263aca2adfffd
Created October 20, 2014 05:02
JS .push() & .pop()
function range(max) {
var retVal = [];
for (var i = 0; i < max; i++) {
retVal.push(i * 2);
}
return retVal;
}
var myArray = range(5);