Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
lizlongnc / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@lizlongnc
lizlongnc / gist:cf4c670543dd31b04c76
Last active August 29, 2015 14:07
JS built-in pseudo parameter (arguments)
// All JS functions receive 2 pseudo parameters - arguments and this.
// The following uses JS built-in pseudo parameter (arguments).
// arguments is an array-like thing that contains all the arguments passed to the function
// if an argument changes, arguments also changes which can sometimes slow things down
// can access arguments.length
// and can use arguments to manipulate values passed to the function
function sum() {
'use strict';
var i = 0, total = 0;
If you using a function with an outside object, use this.
The this pseudo parameters contains a reference to the object of invocation.
this allows a method to know what object it is concerned with.
this allows a single function object to service many objects.
this is key to prototypal inheritance.
allows a single function to work on many objects
the way the function knows which object it is working on is by the this binding
this is a bonus parameter. Its value depends on the calling form.
@lizlongnc
lizlongnc / gist:9b44cc300ddd784a4dbd
Last active August 29, 2015 14:07
JS Function Invocation
The () suffix operator surrounding zero or more comma separated arguments.
The arguments in () passed to the function will be bound to the JS function pseudo parameter - parameters.
If a function is called with too many arguments, the extra arguments are ignored.
If a function is called with too few arguments, the missing values will be undefined.
There is no implicit type checking on the arguments.
There are 4 ways to call a function:
Function form
functionObject(arguments)
@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
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.
(function () {
return expression:
}())
works except if any of the following are used in function
var
function
break
continue
return
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',
@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.
@lizlongnc
lizlongnc / gist:68d1cd7e36abb59c1c42
Last active August 29, 2015 14:07
JS Statements
expression
if
switch
while
do
for
break
continue
return
try/throw