0

I saw this post What is the easiest way to disable/enable buttons and links (jQuery + Bootstrap)

And I really like this:

// Disable function
jQuery.fn.extend({
    disable: function (state) {
        return this.each(function () {
            this.disabled = state;
        });
    }

});

My question is how would I avoid extending $ (or a custom namespace for that matter) See for ref also: https://api.jquery.com/jquery.extend/

I'm looking for safety checks when using extend when it's not needed because obj has already been extended, this to improve performance.

Community
  • 1
  • 1
ItaiRoded
  • 142
  • 7
  • You are right about avoiding to extend if not necessary. Something simple like this, I'd just create a function for it. Whatever design pattern you use, you can easily make it re-usable anyway. However back to your question: you'd have to extend the extend function ;) I'm not sure if that's possible out of the box at all. – twicejr Nov 29 '16 at 22:25
  • If you're using [browsers which support it](http://kangax.github.io/compat-table/es6/#test-Object_static_methods_Object.assign), you could just use [`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) instead of `extend`, or just create your own extend method. Note that the performance difference is likely to be very small. – Heretic Monkey Nov 29 '16 at 22:55

0 Answers0