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
function myfunction(b, context) { | |
// x is equal to 4 | |
context.b = 5; | |
//alert(this); | |
return context; | |
//alert("x1: "+x); | |
// x is now equal to 5 | |
} | |
var x = { |
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
//Reference Types Examples | |
var ref_fn = function () { | |
return true; | |
} | |
, ref_obj = { | |
name: 'obj' | |
} | |
, ref_arr = ['arr', 2, { | |
"p": 3 |
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
(function () { | |
var astr = "[object Array]", | |
toString = Object.prototype.toString; | |
function isArray(a) { | |
return toString.call(a) === astr; | |
} | |
function indexOf(haystack, needle) { | |
var i = 0, |
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
function mem(f) { | |
var cache = {}; | |
return function(){ | |
var key = arguments.length + Array.prototype.join.call(arguments, ','); | |
console.log(arguments);//seems like the arguments refers to those in f. No! it refers to those in return function() | |
if(key in cache) return cache[key]; | |
else return cache[key] = f.apply(this, arguments); | |
}; | |
} |
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
function flexisum(a) { | |
var total = 0; | |
for (var i = 0; i < arguments.length; i++) { | |
var el = arguments[i], | |
num; | |
if (el == null) { | |
continue; | |
} else { | |
if (isArray(el)) { | |
num = flexisum.apply(this, el); //recursion -- deep calculate |
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
function Sandbox() { | |
// turning arguments into an array | |
var args = Array.prototype.slice.call(arguments), | |
// the last argument is the callback | |
callback = args.pop(), | |
// modules can be passed as an array or as individual parameters | |
modules = (args[0] && typeof args[0] === "string") ? args : args[0], | |
i; | |
// make sure the function is called // as a constructor | |
if (!(this instanceof Sandbox)) { |
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
// constructor | |
var Gadget = function (price) { | |
this.price = price; | |
}; | |
// a static method | |
Gadget.isShiny = function () { | |
// this always works | |
 | |
var msg = "you bet"; | |
if (this instanceof Gadget) { |
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
// constructor | |
var Gadget = (function () { | |
// static variable/property | |
var counter = 0, | |
NewGadget; | |
// this will become the | |
// new constructor implementation | |
NewGadget = function () { | |
counter += 1; | |
}; |
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
var constant = (function () { | |
var constants = {}, | |
ownProp = Object.prototype.hasOwnProperty, | |
allowed = { | |
string: 1, | |
number: 1, | |
boolean: 1 | |
}, | |
prefix = (Math.random() + "_").slice(2); |
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
if (typeof Function.prototype.method !== "function") { | |
Function.prototype.method = function (name, implementation) { | |
this.prototype[name] = implementation; | |
return this; // enable chaining function. | |
}; | |
} | |
var Person = function (name) { | |
this.name = name; | |
}. |