Source: talk.md
1/21
Source: talk.md
2/21
History of JavascriptJavaScript has moved from a toy player to a major application languageHistory
Growth of JavaScriptMajor browser vendors in compititon to build a fast engine
Speed ups of a few hundred x over IE 6. Major Server implentations
JavaScript as a Compile TargetJavaScript has of late become the language to use as a compile target
Functions: The best thing in JavaScriptJavaScript Got some things very wrong but did get something quite right: Functions
Syntactic SugarJavaScript is a language with a lot of Syntactic Sugar
Declaring functionsJavaScript has two methods to create a function
ClosuresA JavaScript function created inside a larger function has access to the lexical variables of that function.
Methods of Invocation
Scope: The hidden gotcha in a functionthis is not subject to closure, so unless it is explicitly set it will default to the global window object
Setting this with apply
List OperationsRecent versions of JavaScript feature a full set of operations on Arrays.
Guidelines for use
Map/Filter Example
var list = [2,4,16, 25, 18];
var squareroots = list.filter(isPerfectSquare).map(function (n){
return Math.sqrt(n);
});
Will return this list[2,4,5] A more robust example, extract data from a record
Array.prototype.pick = function (field)
{
return this.filter(function (v){
return v[field]!== undefined;
}).map(function return (v){
return v[field];
});
};
Pattern: Chained OperationsProblemneed to perform several loosely connected operations on data SolutionChain Simple functional operations jQuery uses the chain patterns a lot, by having each method return this it is possible to compose complex behaviors from simple functions.
$('#show').text("Click to Hide").addClass('alert').fadein(4 * 1000).click(function()
{
$(this).fadeout(4 * 1000).removeClass('alert');
return this;
});
Pattern: Function CompostionProblemGiven two functions f and g create a function fg Solution
var compose = function (f, g){
return function (x){
return f(g(x));
};
};
Pattern: Higher Order FunctionsProblemSometimes class of problems will present in which most of the behaviors is repeated but with a small variation SolutionA Higher order function, encode the part that changes as a passed function Pattern: Recursing over an arrayProblemNeed to handle a list of data in time consuming operations (EG Ajax), or where the list could grow. SolutionTake the first element off of the list, process, recursively process the rest of the list var process = function process(list, fn) { if(list.length === 0) { return; } if(list.length === 1) { fn(list[0], function (){}); return; } fn(list[0], function callback(){
process(list.slice(1), fn); Pattern: MonadsMonads are about the most powerful and most misunderstood functional pattern. The Three laws of Monads
When there is a class of functions that take one type and return a different type there are two functions that can be applied to make those functions be subject to composition
Pattern: Monads 2jQuery is a monad$() serves as a wrapping operation wrapping jQuery around DOM elements (unit)$(...).html() return the content of the monad
Source: talk.md
21/21
Table of Contents |