Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Last active August 29, 2015 13:57
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 kevin-lee/9915562 to your computer and use it in GitHub Desktop.
Save kevin-lee/9915562 to your computer and use it in GitHub Desktop.
var logger = console ? console : {
"log": function() {
// It does nothing.
}
};
/**
* Number.prototype.format(n, x, s, c)
*
* @param integer n: length of decimal
* @param integer x: length of whole part
* @param mixed s: sections delimiter
* @param mixed c: decimal delimiter
*/
Number.prototype.format = function(n, x, s, c) {
var regex = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
num = this.toFixed(Math.max(0, ~~n));
return (c ? num.replace('.', c) : num).replace(new RegExp(regex, 'g'), '$&' + (s || ','));
};
/**
* It returns a function that binds the given function with the given context and runs it.
* e.g.)
* function enclosing() {
* this.name = "Kevin";
* var $someElem = $('#someElem');
* $someElem.on('click', _bind_(function(event) {
* event.preventDefault();
* $someElem.val(this.name); // this is now the enclosing function object.
* }, this));
* }
* @param function fn
* @param Object theContext
*/
var _bind_ = function _bind_(fn, theContext) {
return function() {
return fn.apply(theContext, arguments);
};
};
var makeClickable = function makeClickable(selector, callback) {
$(selector).on('click', function(event) {
callback.call(this, event);
}).css('cursor', 'pointer');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment