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
//namespace patter #1 - with comments | |
(function(window, lastObject, private_n){ | |
private_n = function(namespace){ | |
lastObject = lastObject[namespace] = lastObject[namespace] || {}; | |
return private_n; | |
} | |
window.n = function(namespace){ | |
lastObject = window; | |
return private_n(namespace) | |
} |
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
//namespace patter #2 - with comments | |
this.n = function(namespace, namespaceItem, lastObject, namespaceList) { | |
lastObject = this; | |
namespaceList = namespace.split('.'); | |
while(namespaceItem = namespaceList.shift()) { | |
lastObject = lastObject[namespaceItem] = lastObject[namespaceItem]||{}; | |
} | |
} |
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
//observer pattern - with comments | |
(function() { | |
this.o_O = function(obj, eventList, i, item) { | |
eventList = []; | |
obj.subscribe = function(event,fn) { | |
eventList.push(event, fn); | |
} | |
obj.unsubscribe = function(fn) { | |
eventList.splice(eventList.indexOf(fn)-1, 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
//smallest function argument validation + throwing an error if wrong type | |
//is string | |
var a = ""; | |
a.charAt(0); //OK | |
a = 1; | |
a.charAt(0); //TypeError: Object 1 has no method 'charAt' | |
//is number | |
var a = 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
//pubsub pattern | |
(function (eventList, i, item) { | |
eventList = []; | |
this.pubsub = { | |
fire: function (event, a, b, c, d, e) { | |
for (i in eventList) { | |
if (eventList[i] == event) { | |
eventList[+i + 1](a, b, c, d, e); | |
} | |
} |
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
//A little bit buggy, but for me it's ok (work wrong if \t (tab) is used inside of className field) | |
function getElementsByClassPolyfill(className, array, space, index, item){ | |
index = 0, space = " ", className = space + className + space, array = []; | |
while(item = document.all[index++]) { | |
if((space + item.className + space).indexOf(className)+1) { | |
array.push(item); | |
} | |
} | |
return array; | |
} |
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
// http://jsbin.com/igonuh/1/edit | |
function Fx(){return function(){return -1;};} | |
function SafeThis(that){ | |
if (that == window) { | |
return fakeWindow; | |
} else if (that == document) { | |
return fakeDocument; | |
} else { | |
return that; |
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
//namespace patter #3 - with comments | |
this.n = function(s, array, lastItem) { | |
if (array) n(s[array = lastItem.shift()] = s[array] || {}, array, lastItem); | |
else if (!lastItem) n(this, 1, s.split('.')); | |
} | |
//namespace patter #3 - obfuscated (82 chars) | |
this.n=function(a,b,c){b?n(a[b=c.shift()]=a[b]||{},b,c):c||n(this,1,a.split("."))} | |
//namespace patter #3 - usage |
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 builder | |
var functionBuilder = function (args, body) { | |
var argsAsString = args.split(',').toString(); | |
return eval('(function(' + argsAsString + '){' + body + '})'); | |
} | |
//minifed (74 chars) | |
var f=function(a,b){return eval("(function("+""+a.split(",")+"){"+b+"})")} | |
//sample usage |
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 a = [ 1, 2, 3, 2 ]; | |
console.log(PseudoLinq(a).select(function(i){ | |
return i + 1 | |
}).toArray()); | |
console.log(PseudoLinq(a).where(function(i){ | |
return i > 1 | |
}).select(function(i){ | |
return i - 1 |
OlderNewer