Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Common useful accessories for javascript
//Some functions where taken from Jistin Windle's sketch.js at https://github.com/soulwire/sketch.js
// Modulus with support for negative integers
Mod = function(current, limit) {
return ( (current % limit) + limit ) % limit;
};
function isArray( object ) {
return Object.prototype.toString.call( object ) == '[object Array]';
}
function isFunction( object ) {
return typeof object == 'function';
}
function isNumber( object ) {
return typeof object == 'number';
}
function isString( object ) {
return typeof object == 'string';
}
function extend( target, source, overwrite ) {
for ( var key in source )
if ( overwrite || !( key in target ) )
target[ key ] = source[ key ];
return target;
}
function proxy( method, context ) {
return function() {
method.apply(context, arguments);
}
}
function clone( target ) {
var object = {};
for ( var key in target ) {
// not sure why is not using hasOwnProperty
if ( isFunction( target[ key ] ) )
object[ key ] = proxy( target[ key ], target);
else
object[ key ] = target[ key ];
}
return object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment