Skip to content

Instantly share code, notes, and snippets.

View gkatsev's full-sized avatar

Gary Katsevman gkatsev

View GitHub Profile
@gkatsev
gkatsev / objcreate.js
Created May 27, 2013 22:11
Object.create in JS.
var foo = {};
var bar = Object.create(foo);
var Ctor = (function(window, undefined) {
var private = 5;
var Ctor = function() {
// ...
}
Ctro.protoype.foo = function() {
return private;
}
@gkatsev
gkatsev / loadscript.js
Created May 10, 2013 23:48
A simple script loader
// loadScript :: String, function, predicate [, arg1 [, arg2 [, ...]]] -> undefined
// The optional arguments will get passed to the callback
var loadScript = (function() {
var scripts = {};
return function(url, callback, predicate) {
var
body = document.body,
script = document.createElement('script'),
loaded = false,
if (this.player_.muted()) {
return 0;
} else {
return this.player_.volume();
}
@gkatsev
gkatsev / gist:5456373
Created April 24, 2013 23:14
preventDefault a click on android
if (/android/i.test(navigator.userAgent) && !/chrome/i.test(navigator.userAgent)) {
this.on('click', function(event) {
event.preventDefault();
event.stopPropagation();
});
} else {
this.on('touchend', function(event) {
event.preventDefault();
event.stopPropagation();
});
@gkatsev
gkatsev / variadic.js
Last active December 13, 2015 23:08
Create a variadic function that uses function's arguments as variables.
(function(x, y, z) {
"use strict";
x = y = 0;
z = arguments.length;
for (; y < z; y++) {
x += arguments[y];
}
@gkatsev
gkatsev / strictMode.md
Last active December 12, 2015 00:39
An overview and features of strict mode

Strict Mode

Overview

Strict mode is an opt-in mode for JavaScript that fixes, disables, and changes some of the most problematic features in the language.

Strict mode is invoked with "use strict"; statement at the top of the current scope. So, it works both in functions and in files. It is better to only ever specifiy strict mode inside of functions because this simplifies concatenating

String.prototype.splitSome = function(del, n){
var a = this.split(del)
, b = a.slice(n).join(del)
a.length = n
a[a.length] = b
return a
};
"one,two,three,four,five".splitSome(',', 3)
@gkatsev
gkatsev / httpcors
Created September 27, 2012 20:28
simple python http server with CORS enabled
#!/usr/bin/env python
import SimpleHTTPServer
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
(function (n) {
var f = '',
s = n < 0 ? '-' : f,
p = [],
l = 0,
m = 4294967296,
o = m * 1024,
a = Math.floor;
o *= o;
n = a(s ? -n : n);