0

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);

Community
  • 1
  • 1
haxxerz
  • 893
  • 6
  • 17

1 Answers1

3

A function is also an object:

function foo() { /* blah */ }
foo.bar = function () { /* blah */ }
wroniasty
  • 7,884
  • 2
  • 32
  • 24
  • Cool - another syntax q for you, since I don't have privs to post more than once / 25 mins: http://pastehtml.com/view/c6aiy4slm.mrk =) – haxxerz Jul 28 '12 at 08:31
  • `typeof` is an operator: https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof – wroniasty Jul 28 '12 at 08:46