Skip to content

Instantly share code, notes, and snippets.

@stellaraccident
Created January 25, 2011 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stellaraccident/795853 to your computer and use it in GitHub Desktop.
Save stellaraccident/795853 to your computer and use it in GitHub Desktop.
Some boilerplate JavaScript bits from otherwise not published sources
/**
* rcutil.ecmabackport.js
* Backport various new ecmascript functions to older runtime
* environments.
*/
(function() {
var _Object=Object, _FunctionPrototype=Function.prototype,
_Array=Array, _ArrayPrototype=_Array.prototype;
// Take advantage of old ecmascript defaults of the global
// object becoming this for naked functions. Does not happen
// in ecmascript strict mode, but then ecmascript strict mode
// will always have a global reference
if (this&&!this.global) {
this.global=this;
}
// Feature detect
if (_Object.create && _FunctionPrototype.bind) return;
function define(target, name, value) {
if (!target[name]) target[name]=value;
}
function createFunction() {
return function() { }
}
// Add backported Object.* methods. Do a feature detect on Object.create
// and only do it if not exists
define(_Object, 'create', function(O) {
var ctor;
if (O) {
ctor=createFunction();
ctor.prototype=O;
return new ctor();
}
return {};
});
define(_Object, 'keys', function(O) {
var ary=[], k;
for (k in O) {
if (O.hasOwnProperty(k)) ary.push(k);
}
return ary;
});
// Function builtins
function bindThis(fcn, thisArg) {
return function() {
return fcn.apply(thisArg, arguments);
}
}
function bindThisPlusArgs(fcn, thisArg, args) {
return function() {
var fullArgs=args.concat(_ArrayPrototype.slice.call(arguments,0));
return fcn.apply(thisArg, fullArgs);
}
}
define(_FunctionPrototype, 'bind', function(thisArg /*, args */) {
if (arguments.length===1) return bindThis(this, thisArg);
else return bindThisPlusArgs(this, thisArg,
_ArrayPrototype.slice.call(arguments, 1));
});
// Array builtins
define(_Array, 'isArray', function(ary) {
return ary instanceof _Array;
});
define(_ArrayPrototype, 'every', function(cb, thisArg) {
var i, l=this.length;
for (i=0; i<l; i++) {
if (!this.hasOwnProperty(i)) continue;
if (!cb.call(thisArg, this[i], i, this)) return false;
}
return true;
});
define(_ArrayPrototype, 'some', function(cb, thisArg) {
var i, l=this.length;
for (i=0; i<l; i++) {
if (!this.hasOwnProperty(i)) continue;
if (cb.call(thisArg, this[i], i, this)) return true;
}
return false;
});
define(_ArrayPrototype, 'forEach', function(cb, thisArg) {
var i, l=this.length;
for (i=0; i<l; i++) {
if (!this.hasOwnProperty(i)) continue;
cb.call(thisArg, this[i], i, this);
}
});
define(_ArrayPrototype, 'map', function(cb, thisArg) {
var i, ret=[], l=this.length;
for (i=0; i<l; i++) {
if (!this.hasOwnProperty(i)) continue;
ret[i]=cb.call(thisArg, this[i], i, this);
}
return ret;
});
define(_ArrayPrototype, 'filter', function(cb, thisArg) {
var i, ret=[], l=this.length, v, to=0;
for (i=0; i<l; i++) {
if (!this.hasOwnProperty(i)) continue;
v=this[i];
if (cb.call(thisArg, v, i, this))
ret[to++]=v;
}
return ret;
});
define(_ArrayPrototype, 'reduce', function(cb, iv) {
var i, l=this.length;
if (arguments.length===1) {
iv=this[0];
i=1;
}
for (; i<l; i++) {
if (!this.hasOwnProperty(i)) continue;
iv=cb(iv, this[i], i, this);
}
return iv;
});
define(_ArrayPrototype, 'reduceRight', function(cb, iv) {
var i=this.length-1;
if (arguments.length===1) {
iv=this[i--];
}
for (; i>=0; i--) {
if (!this.hasOwnProperty(i)) continue;
iv=cb(iv, this[i], i, this);
}
return iv;
});
})();
/**
* Takes a constructor written according to the following pattern and
* updates its prototype chain to inherit from the given superCtor's
* prototype.
* <pre>
* function BaseClass() {
* }
* BaseClass.prototype={
* // base class properties
* };
*
* function DerivedClass() {
* BaseClass.call(this);
* }
* DerivedClass.prototype={
* // derived properties
* };
* inherits(DerivedClass, BaseClass)
* </pre>
*
* Note that the original DerivedClass.prototype is discarded as part of this
* operation, being used solely to initialize the newly created inherited
* prototype.
* @public
*/
function inherits(ctor, superCtor) {
var newProto=Object.create(superCtor.prototype);
copy(newProto, ctor.prototype);
ctor.prototype=newProto;
};
/**
* Copy all own properties from src to dest.
* @return dest
*/
function copy(dest, src) {
for (var k in src) {
if (src.hasOwnProperty(k))
dest[k]=src[k];
}
}
// exports
exports.inherits=inherits;
exports.copy=copy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment