haxxerz
haxxerz

Reputation: 943

How to define function and object of same name?

So according to this, it isn't possible. But I call bullshit, because the jQuery object, for instance, is able to be called as an object with member functions (jQuery.cookie()), but also as a function itself (jQuery('my_selector')).

How can I achieve the same effect with my objects? Let's say that I want an object named foo in the window namespace, with a member function bar, but also a function named foo in the same window namespace as the foo object. After declaring such an object-function hybrid, I'll be able to make the following calls:

window.foo(arg);

window.foo.bar(arg);

Upvotes: 0

Views: 197

Answers (1)

wroniasty
wroniasty

Reputation: 8052

A function is also an object:

function foo() { /* blah */ }
foo.bar = function () { /* blah */ }

Upvotes: 3

Related Questions