Skip to content

Instantly share code, notes, and snippets.

View cms's full-sized avatar

Christian C. Salvadó cms

View GitHub Profile
@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 / 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 / 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]';
};
})();
}
/**
* 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 / 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":}'
@cms
cms / JSON.js
Created October 6, 2010 08:04
Added reviver support to Asen's JSON.js
/**
* Written by Asen Bozhilov
* http://code.google.com/p/js-examples/source/browse/trunk/JSON.js
*/
if (!this.JSON) {
this.JSON = {
parse : (function (undefined) {
var JSON_GRAMMAR = {
STRING : '"([^"\\x00-\\x1F\\\\]|\\\\["\\\\\/bfnrt]|\\\\u[0-9A-Fa-f]{4})*"',
@cms
cms / BESEN-String-index-named-properties.js
Created September 11, 2010 08:27
BESEN bug on String index named properties
/**
* BESEN bug on String index named properties
*
* There are some problems accessing the named properties of String
* objects that correspond to the individual characters of its
* [[PrimitiveValue]].
*
* Tested on BESEN r117
*/
@cms
cms / besen-logicaloperators.js
Created September 8, 2010 07:39
BESEN wrong behavior of the logical && and || operators
// BESEN wrong behavior of the logical && and || operators
// Tested on r111
var obj = {};
// Logical AND operator
print(obj && obj); // prints true, expected '[object Object]'
print(obj && 'foo'); // prints '[object Object]', expected 'foo'
print(obj && 4); // prints '[object Object]', expected 4
print(obj && NaN); // prints false, expected NaN
@cms
cms / es-harmony_array_subclassing.js
Created August 18, 2010 08:10
Generic Array subclassing function using ECMA-Harmony Proxy features
// Generic function to subclass an array in JavaScript
// (using the ECMAScript Harmony Proxy features)
// Structuring of code inspired by @kangax's
// array_subclassing (http://github.com/kangax/array_subclassing)
var makeSubArray = (function () {
var MAX_UINT32 = Math.pow(2, 32) - 1;
return function (properties) {
properties || (properties = {});
@cms
cms / newApply.js
Created August 17, 2010 08:21
newApply
// newApply: Invoke a consturctor function correctly, passing an arbitrary
// number of arguments
function newApply(fn, argsArray) { // (c) Christian C. Salvadó - MIT License
var F = newApply, obj, ret, t, isPrimitive;
if (this instanceof F) return; // this function is used as a dummy constructor
F.prototype = fn.prototype; // set up the prototype and constructor properties
F.prototype.constructor = fn;