アプリで jQuery のようなアーキテクチャを作成しようとしていますが、期待した結果を得ることができませんでした。
jQuery では、「jQuery オブジェクトは、実際には「強化された」init コンストラクターにすぎません」。
jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
},
これは、jQuery オブジェクトを次のように開始するときを意味します。
$('selector')
jQuery は
new jQuery.fn.init( selector, context, rootjQuery );
jQuery プロトタイプは次のように定義されています。
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
...
return jQuery.makeArray( selector, this ); // An array
}
...
size: function() {
return this.length;
},
...
jQuery のすべての便利なプロパティとメソッド (hide()、show() など) は、jQuery オブジェクトのプロトタイプによって保持されます。
そして、init メソッドのプロトタイプが jQuery のプロトタイプとして割り当てられます。
jQuery.fn.init.prototype = jQuery.fn;
良い!。私の問題は、このアーキテクチャを使用しようとしたが、戻り値のプロパティとメソッドを取得できなかったことです。
これが私のコードです。
(function() {
Metinler = function(MetinKodu){
return new Metinler.sub.baslat( MetinKodu );
}
Metinler.sub = Metinler.prototype = {
metinKodlari: [],
constructor: Metinler,
topla: function(){
return this.metinKodlari[0] + this.metinKodlari[1];
},
baslat: function(MetinKodu) {
if($.isArray(MetinKodu) && MetinKodu.length > 0) {
this.metinKodlari = MetinKodu;
}else{
this.metinKodlari = (MetinKodu) ? [MetinKodu] : [''];
}
return this.metinKodlari;
}
}
Metinler.sub.baslat.prototype = Metinler.sub;
window.Metinler = Metinler;
})()