jQuery は、そのコンストラクターが引数を受け取る関数として機能する一方で、そのコンストラクターが引数を受け取る関数としても機能することをどのように許可しますか?
私はJavaScriptに少し慣れていないので、これが初心者の質問である場合はすみません(ソースを見ましたが、分析するのは難しいです).
とにかく、例として$(document).ready(<args>);
コンストラクタ$()
とプロトタイプの両方がready()
関数として機能します。どのように?私がこれを試してみると:
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!") });
エラーが発生するUncaught TypeError: Object[object global] has no method 'ready'