简体   繁体   中英

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

A function is also an object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM