This file contains hidden or 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 MYAPP = MYAPP || {}; | |
| MYAPP.namespace = function (ns_string) { | |
| var parts = ns_string.split('.'), | |
| parent = MYAPP, | |
| i; | |
| // strip redundant leading global | |
| if (parts[0] === "MYAPP") { | |
| parts = parts.slice(1); |
This file contains hidden or 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 hidden or 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 hidden or 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 Gadget() { // private member | |
| //var specs = { | |
| var screen_width = 320, | |
| screen_height = 480, | |
| color = "white" | |
| // }; | |
| // public function | |
| this.getSpecs = function () { | |
| //return new object to protect variable privacy | |
| return { |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | |
| }; |