Skip to content

Instantly share code, notes, and snippets.

@vanrez-nez
Last active March 9, 2018 19:37
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 vanrez-nez/1e2f88c26acf9cd6b17c to your computer and use it in GitHub Desktop.
Save vanrez-nez/1e2f88c26acf9cd6b17c to your computer and use it in GitHub Desktop.
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