Skip to content

Instantly share code, notes, and snippets.

View cms's full-sized avatar

Christian C. Salvadó cms

View GitHub Profile
@cms
cms / firefox-stringify-bug.js
Created October 6, 2010 18:09
JSON.stringify broken on Firefox
var obj = { foo: function () { return "bar"; } };
var jsonString = JSON.stringify(obj, function (key, value) {
if (typeof value == 'function') {
return value();
}
return value;
});
// Firefox <= 3.6, produces invalid JSON: '{"foo":}'
/**
* This library defines a new style ES objects
* which support delegation based mixins. A mixin
* chain is stored in the internal [[Mixin]] property.
*
* Used features: Harmony (ES6) proxies.
*
* Tested in FF4 beta.
*
* @author Dmitry A. Soshnikov <dmitry.soshnikov@gmail.com>
@cms
cms / Array.isArray.js
Created October 11, 2010 15:16
Array.isArray
if (typeof Array.isArray != 'function') {
Array.isArray = (function () {
var toString = Object.prototype.toString;
return function (obj) {
return toString.call(obj) == '[object Array]';
};
})();
}
@cms
cms / WebKit-strict-mode.js
Created October 13, 2010 17:17
WebKit strict mode bugs
// WebKit strict mode bugs
// Eval code should be "strict eval code" if the call to eval is a direct call (15.1.2.1.1) to
// the eval function that is contained in strict mode code:
(function () {
"use strict";
return eval('(function() { return !this; })()');
})();
@cms
cms / Object.getPropertyNames.js
Created October 19, 2010 07:35
ES6 Object.getPropertyNames
Object.getPropertyNames ||
Object.defineProperty(Object, "getPropertyNames", {
value: function(obj) {
var propertyNames = [];
do {
propertyNames.push.apply(propertyNames, Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
} while (obj);
// get unique property names
obj = {};
@cms
cms / proto-array-sandbox.js
Created October 27, 2010 15:36
Simple `__proto__` based Array sandbox
var sb = {
Array: function() {
var result = Array.apply(null, arguments);
result.__proto__ = sb.Array.prototype;
return result;
}
};
sb.Array.prototype = [];
sb.Array.prototype.last = function () { return this[this.length -1]; };
@cms
cms / isStrictFunction.js
Created March 2, 2011 06:35
isStrictFunction
function isStrictFunction(fn) {
if (typeof fn != 'function') throw "Not a function";
try {
fn.caller, fn.arguments;
return false;
} catch (e) {
return e instanceof TypeError;
}
}
@cms
cms / Function.prototype.bind.js
Created August 15, 2011 17:08
Function.prototype.bind shim
if (typeof Function.prototype.bind != 'function') {
Function.prototype.bind = (function () {
var slice = Array.prototype.slice;
return function (thisArg) {
var target = this, boundArgs = slice.call(arguments, 1);
if (typeof target != 'function') throw new TypeError();
function bound() {
var args = boundArgs.concat(slice.call(arguments));
@cms
cms / v8-array#push-used-internally.js
Created August 19, 2011 07:20
V8 Engine internals using built-ins
/* V8 Engine internals using built-ins
*
* Bug report:
* http://code.google.com/p/v8/issues/detail?id=1625
* Bug on code:
* http://code.google.com/p/v8/source/browse/trunk/src/v8natives.js?r=8733#1026
* http://www.google.com/codesearch#W9JxUuHYyMg/trunk/src/v8natives.js&l=1026,1029
*/
var _push = Array.prototype.push;
@cms
cms / wrap.js
Created September 7, 2011 02:58
var wrap = function (fn, wrapper) {
var params = [], len = fn.length;
while (len--) {
params[len] = 'a'+len;
}
return Function('fn,wrapper',
'return function ('+params+') {' +
' return wrapper.call(this, fn, [].slice.call(arguments));'+
'}')(fn, wrapper);