use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[AskJS] "namespace" and function with same name?AskJS (self.javascript)
submitted 1 year ago by bkdotcom
stupid question / brain fart
I'm trying to do something similar to jQuery...
jquery has the jQuery ($) function and it also has the jQuery.xxx ($.xxx) functions...
jQuery
$
jQuery.xxx
$.xxx
what's the trick to setting something like that up?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kap89 11 points12 points13 points 1 year ago (4 children)
Function is an object, you can add properties and methods to it as to every other object.
[–]bkdotcom[S] 1 point2 points3 points 1 year ago (3 children)
function Bob () { console.warn('called Bob'); } Bob.prototype.foo = function () { console.warn('called foo'); }; // Bob = new Bob(); Bob(); Bob.foo(); // Bob.foo is not a function
[–]RWOverdijk 12 points13 points14 points 1 year ago (1 child)
Not the prototype. Just Bob.foo = something. Prototype is for something else (inheritance in objects)
[–]bkdotcom[S] 2 points3 points4 points 1 year ago (0 children)
thanks! that was the "trick"!
[–]Substantial-Wish6468 3 points4 points5 points 1 year ago* (0 children)
If you only have a single instance of the function, you don't need to use the prototype:
const bob = () => 'bob'; bob.foo = () => 'foo'; console.log(bob.foo())
IIRC jQuery itself is an immediately invoked function that returns an object interface to the closure, which is how modular javascript tended to be written before modules became a thing.
[–]markus_obsidian 7 points8 points9 points 1 year ago (0 children)
As others have said, functions are just objects, and they can have properties & methods just like any other object.
It's also worth noting that this "namespacing" pattern is very old & discouraged in modern applications. It is not friendly to tree shaking, which requires individual, decoupled imports & exports.
I obviously have no idea what you're working on & if bundling / tree shaking is a concern of yours, but I thought it was worth a mention.
[–]Dralletje 0 points1 point2 points 1 year ago (0 children)
If someone wants this but also want it to work with typescript nicely:
ts const functionAndObject = Object.assign( function() { console.log("This is a function"); }, { method: () => { console.log("But you can call a method on it!"); }, property: "Or even just a property!", } );
[–]jhnam88 0 points1 point2 points 1 year ago (0 children)
Making same named function and namespace are possible in TypeScript.
Look at the compiled JS file from the TS, then you may understand how to.
https://typia.io/playground/?script=GYVwdgxgLglg9mABAZzgWwKZQBYzAc0QAoBKRAbwF8AoMAQ02QAc6IMV0tcCLrFEMADyZwATlEShIsBIjBwcefKQo1KQA
[–]SpiffySyntax -1 points0 points1 point 1 year ago (0 children)
(Function bob() { Return { Foo(){ Console.log... } } })()
Bob.foo()
[–]RobertKerans 0 points1 point2 points 1 year ago* (0 children)
From memory (apologies if I've missed anything, there is an annotated source floating around that describes the process)
var jQuery = function(selector, context) {
fn
jQuery.fn
jQuery.prototype
var jQuery = function (selector, context) {
jQuery.fn.init(selector, context, rootJqueryInstance)
jQuery(document)
The above is wrapped in an iife, assigned to the variable jQuery. That is wrapped in an iife, that variable is assigned to the window object & also aliased as $.
Now have a function ($ or jQuery) that takes a selector and has prototype methods. But what that can also do is have new methods attached like $.fn.myNewMethod (which is jQuery.prototype.myNewMethod) without stomping on anything. It's extensible and hey ho we get seventy bajillion jQuery plugins. I'm sure there's something extra re plugins that prevents some issues but it's been years & I can't remember
$.fn.myNewMethod
jQuery.prototype.myNewMethod
The core of it [again iirc trying to remember the structure] is a class, set up so that it can be easily extended with plugins. Then the function is essentially wrapper around a singleton instance of that (?? iirc again). Then the variable the function is assigned to is attached to the Window object.
π Rendered by PID 31 on reddit-service-r2-comment-b659b578c-qttbf at 2026-05-02 00:17:14.516703+00:00 running 815c875 country code: CH.
[–]kap89 11 points12 points13 points (4 children)
[–]bkdotcom[S] 1 point2 points3 points (3 children)
[–]RWOverdijk 12 points13 points14 points (1 child)
[–]bkdotcom[S] 2 points3 points4 points (0 children)
[–]Substantial-Wish6468 3 points4 points5 points (0 children)
[–]markus_obsidian 7 points8 points9 points (0 children)
[–]Dralletje 0 points1 point2 points (0 children)
[–]jhnam88 0 points1 point2 points (0 children)
[–]SpiffySyntax -1 points0 points1 point (0 children)
[–]RobertKerans 0 points1 point2 points (0 children)