Skip to content

Instantly share code, notes, and snippets.

@abozhilov
abozhilov / gist:1042640
Created June 23, 2011 14:30
Array.prototype.unique
Array.prototype.unique = (function () {
var TYPE_MAP = {
'object' : 1,
'function' : 1
}
return function () {
var map = {},
objects = [],
unique = [];
for (var i = 0, len = this.length; i < len; i++) {
@abozhilov
abozhilov / gist:1127225
Created August 5, 2011 09:45
Function.prototype.bind
Function.prototype.bind = function (thisVal) {
var target = this,
args = [].slice.call(arguments, 1);
function f () {
return target.apply(
this instanceof f ? this : thisVal,
args.concat([].slice.call(arguments))
);
};
f.prototype = this.prototype;
@abozhilov
abozhilov / gist:1161984
Created August 22, 2011 09:09
RegExp.prototype.match
/**
* Applying regex on substring
* http://qfox.nl/weblog/237
*/
RegExp.prototype.match = function (str, start) {
if (start > 0) {
return this.exec(str.slice(start));
}
return this.exec(str);
};
@abozhilov
abozhilov / gist:1170259
Created August 25, 2011 08:49
Array.prototype.toString - ECMAScript5
// In ECMA-262 5th edition all methods of Array.prototype are intentionally generic. It means if you pass different
// native object than array for the this value, the particular method will work in same way as for array instances.
// ECMA-262 does not define how those methods will work if you pass a host object for the this value. That behavior // is implementation dependent.
// For example:
var arr = [];
Array.prototype.push.call(arr, 1, 2);
arr[0]; //1
arr[1]; //2
@abozhilov
abozhilov / gist:1230188
Created September 20, 2011 20:14
Radio player
#!/bin/bash
#Name: Radio player
#Author: Asen Bozhilov
#Version: 1.0.0
#License: MIT
#Available radio streams
RADIO_NAMES=(
"Дарик Радио"
"Радио 1"
@abozhilov
abozhilov / gist:1235894
Created September 22, 2011 20:12
Memory leak
// creates a script load listener
function create_script_load_listener(elem, registry_item, flag,onload) {
/**
* If the garbage collector use reference count for the host objects
* code below will eventually leak if `onload' or `onreadystatechange' hasn't triggered.
* `onload' and `onreadystatechange' create circular reference trought the internal [[Scope]]
* elem -> onload -> [[Scope]] -> Variable Object -> elem
*/
elem.onload = elem.onreadystatechange = function() {
@abozhilov
abozhilov / gist:1333507
Created November 2, 2011 12:32
Arguments default value
function func(a, f) {
return function (args) {
args.__proto__ = a;
f.call(this, args);
};
};
var f = func({foo : 10, bar : 20}, function (args) {
print(args.foo, args.bar);
@abozhilov
abozhilov / gist:1336141
Created November 3, 2011 09:38
Cross browser default parameter values.
var def = (function () {
var PROTO_SUPPORT = !{__proto__ : null}.toString,
hasOwnP = {}.hasOwnProperty;
var def;
if (PROTO_SUPPORT) {
def = function (params, func) {
return function (args) {
args.__proto__ = params;
func.call(this, args);
@abozhilov
abozhilov / gist:1339681
Created November 4, 2011 15:55
setPrototypeOf
function setPrototypeOf(obj, proto) {
var p = proto;
do {
if (obj === p) {
throw Error('Circular prototype chain');
}
} while (p = Object.getPrototypeOf(p));
obj.__proto__ = proto;
return obj;
@abozhilov
abozhilov / gist:1670090
Created January 24, 2012 13:03
Kik's extension check
var IMG_EXT = {
'.jpg' : 1,
'.png' : 1,
'.gif': 1,
'.jpeg' : 1
};
var ext;
if (IMG_EXT[(ext = path.slice(-5))] || IMG_EXT[ext.slice(1)]) {
//do something