This is a difficult one to explain, so I welcome comments querying the details. Basically with jQuery you can do the following things:
.
What I want
$.ajax().etc()
$("selector").doStuff().etc()
Which means that $ is acting as a function (that is then chainable) as well as an object (also chainable).
.
What I’ve got
I am writing some javascript myself and have successfully made a chainable set of functions like so:
myF.func1().func2()
myF('text') //Cannot get this working!
.
I’m using window.myF=(new myFuncs()) to get the first line working, but I can’t then use myF as a function. I’ve also made it so that myF can be used as a function, but then I can’t chain the other functions.
I’m extremely confused and as much as I’ve tried searching this site and Google, I must be searching the wrong thing as I have no idea where to go from here!
Questions in the comments are welcomed and expected!
.
My Setup (simplified)
(function(){
var myFuncs=function(){
};
myFuncs.prototype = {
foo: function() {
}
,bar: function() {
}
}
window.myF = (new myFuncs());
})();
I had a quick look at the jQuery source code and it looks like it doesn’t use prototypes for the jQuery object – the
window.$.Instead it uses
$.extend:However, for what I’ll call the jQuery response object it does use prototypes. So if you call
myF()this code gets run:$.postis an object property, you can’t get it if you call the function itself.$().htmlis a jQuery response object function, it is not a member of thewindow.$object.Answer to your update: http://jsfiddle.net/rmpW8/