jQuery.sub() (alias $.sub) created a new copy of jQuery whose methods you could change without affecting the global jQuery / $ object. It took no arguments and returned that copy (jQuery 1.5–1.8).
Syntax
Start with these common calls. Keep them short and readable.
jQuery
var sub$ = jQuery.sub(); // copy of jQuery
sub$.fn.myCustomMethod = function () {
return "just for me";
};
sub$("body").myCustomMethod(); // "just for me"
typeof jQuery("body").myCustomMethod; // undefined
// Not a sandbox — events / Ajax / data still use main jQuery
Call
What it does
jQuery
Learn jQuery.sub(): create a modifiable copy of jQuery for private methods and plugin encapsulation, override without touching global $, deprecated in 1.7 removed in 1.9, examples, pitfalls, and FAQs.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Call var copy = jQuery.sub() to get a private jQuery function.
Step 2
Add or override methods on copy.fn (not on global jQuery.fn).
Step 3
Use copy(selector) (or copy(this)) so chains see your private methods.
Return Value
Return Value
Description
See examples
Check the examples below for what this API returns in common cases.
jQuery Examples
Example 1 — Private Method on a Sub
Add a method that exists only on the copy — not on global jQuery.
jQuery
(function () {
var sub$ = jQuery.sub();
sub$.fn.myCustomMethod = function () {
return "just for me";
};
console.log(sub$("body").myCustomMethod()); // "just for me"
})();
console.log(typeof jQuery("body").myCustomMethod); // undefined
Fork.myjQuery is a copy; global $.fn.remove is unchanged.
Wrap. The override fires a "remove" event, then calls the real remove.
Scope. Only removals done through myjQuery get the extra event.
Example 3 — Plugin That Returns Sub Methods
Expose open / close only after calling .myplugin().
jQuery
var plugin = jQuery.sub();
plugin.fn.extend({
open: function () { return this.show(); },
close: function () { return this.hide(); }
});
jQuery.fn.myplugin = function () {
this.addClass("plugin");
return plugin(this); // chain sees open/close
};
$("#main").myplugin().open();
// $("#main").open() alone would fail — open is on the sub only
opened via myplugin().open()
typeof jQuery.fn.open → undefined
typeof $panel.open → function
How It Works
Private API.open / close live on plugin.fn, not global jQuery.fn.
Entry point..myplugin() is public and returns plugin(this).
Chain. After .myplugin(), .open() works because the collection is the sub type.
Frequently Asked Questions
jQuery.sub() returned a new copy of the jQuery function whose properties and methods you could change without modifying the global jQuery / $ object. Private plugin methods and method overrides lived on that copy. Added in jQuery 1.5.
No. It was deprecated in jQuery 1.7 and removed in jQuery 1.9. Modern jQuery 3.x does not include $.sub(). Try-it labs on this page load jQuery 1.8.3 so you can still run authentic historical demos.
No. Official docs stress that sub() is not sandbox isolation. Events, .data(), and Ajax still go through the main jQuery. Only the jQuery function object and its fn methods are copied so you can extend or override them privately.
Two cases: (1) override methods like .remove() on a private copy without changing global jQuery, and (2) basic plugin encapsulation so helper methods exist only on the subbed instance. For new plugins, prefer the jQuery UI widget factory or plain modules.
noConflict() restores the previous global $ and can release the jQuery name — it does not create a fork of the API. sub() kept the same global jQuery and gave you a second, editable copy for private extensions.
Use ES modules or IIFEs for private helpers, jQuery.fn plugins that return the original collection, or the jQuery UI widget factory for stateful plugins. Do not rely on $.sub() in new code — it is gone from modern jQuery.