How would jQuery allow its constructor to act as a function accepting arguments meanwhile its constructor also acts as a function accepting arguments?
I am a bit new to JavaScript, so excuse me if this is a noob question, (I have looked at the source but it's hard to try to dissect).
Anyway, as an example $(document).ready(<args>);
Both the constructor $()
and the prototype ready()
act as a function. How? Because if I try this:
var $ = function( selector ) {
if(selector == document) {
return document;
}
};
$.prototype = {
constructor: $,
ready: function( args ) {
if( isDomReady ) {
args.apply( document );
} else {
window.onload = args;
}
}
};
var isDomReady = ( document.addEventListener || document.readyState == ("complete"|"loaded"|true|4) || document.onreadystatechange() ) ? true : false;
$(document).ready(function() { alert("Wibbles!") });
I get an error Uncaught TypeError: Object[object global] has no method 'ready'