Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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:68d1cd7e36abb59c1c42
Last active August 29, 2015 14:07
JS Statements
expression
if
switch
while
do
for
break
continue
return
try/throw
@lizlongnc
lizlongnc / gist:a10cd61bc4b8d80c7ad5
Last active August 29, 2015 14:07
JS .toString()
number.toString(radix);
number can be a variable
radix - optional and determines the base to use for representing a numeric value. Must be an integer between 2 and 36.
2 - binary
8 - octal
16 - hexadecimal- often used with hex values used in styling such as when changing color, background-color etc.
Closure is a central idea in JavaScript.
The context of an inner function includes the sclope of the outer function.
An inner function enjoys that context even after the parent functions have returned.
function scope works like block scope.
Global:
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
(function () {
return expression:
}())
works except if any of the following are used in function
var
function
break
continue
return
Recursion - when a function calls itself.
Discovered with LISP around 1958.
A great example of recursion is the quicksort
algorithm:
1. use quicksort to divide the array into two groups, low and high
if alpha, put all items that begin with a-m into 1st group and the remaining in 2nd group
if numbers, determine the mid-point and put lower nums in 1st group and the remaining in 2nd group unless this type of dividing would result in one group being much larger than the other, if so, adj mid-point
2. call quicksort again on each group containing more than one element.
@lizlongnc
lizlongnc / gist:6e7cddbac1f8f75c226b
Last active August 29, 2015 14:07
JS Functions
Where did functions come from?
subroutines
In earlier languages, subroutines were written and used in various ways.
Subroutines were called and could return something.
They were also referred to as: sub, procedure, proc, func, functions, lambda and are all the same idea.
What advantages do subroutines give us?
code reuse which was originally a memory conserving device
decomposition - take a complicated problem and break it down into subroutines