2012-06-19 31 views
6

Ben sadece çözmek için görünmüyor garip bir sorun, abit var! Yazdığım büyük bir çerçevenin bir parçası, ama aynı soruna sahip bazı test kodları yazdım. Aşağıdaki bakınız:Javascript Türü Hata, bir fonksiyon değil

!function ($, window, undefined) { 

    // BASE FUNCTION 
    var test = function (selector, context) { 
     return new test.fn.init(selector, context); 
    }; 

    // SELECTOR FUNCTIONS 
    test.fn = { 
     selector: undefined, 
     init:  function (selector, context) { 
      // Use jQuery to build selector object 
      this.selector = $(selector, context); 
      return this; 
     }, 

     // Create a popup dialog 
     popup:  function (options) { 
      this.selector.dialog(); 
     } 
    }, 

    // Expose Carbon to the global object 
    window.test  = test; 

}(window.jQuery, window); 

Şimdi şu kullandığınızda:

test('#popupLink').popup(); 

alıyorum: "açılan bir işlev değil "TypeError testi (" # popupLink)".

test('#popupLink').selector.hide(); 

Herhangi bir yardım büyük takdir şu anda bir akıl blok yaşıyorum gibi: Ben böyle bir şey yaparsanız ben standart jQuery fonksiyonları kullanırım gibi kısmen, çalışma biliyorum. Şimdiden teşekkürler! :)

Güncelleme

Ben döndürülen nesne görüntülemek için console.log kullandım ve sadece ben prototip kullanmak vermedi gibi mantıklı içinde seçici elemanı vardır. Bunu nasıl düzeltebilirim?

+0

Test.prototype veya test.fn.prototype için test.fn'yi değiştirin – jasssonpet

+0

"test.fn.prototype = test.fn" ifadesini ekledim, ancak yine de aynı hata veriyor :( – jleck

+0

You –

cevap

3
(function ($, window) { 
    // BASE FUNCTION 
    var test = function (selector, context) { 
     return new test.fn.init(selector, context); 
    }; 

    // SELECTOR FUNCTIONS 
    test.fn = test.prototype = { 
     constructor: test, 
     init: function (selector, context) { 
      // Use jQuery to build selector object 
      this.selector = $(selector, context); 
      return this; 
     }, 

     // Create a popup dialog 
     popup: function (options) { 
      console.log('popup'); 
      return this; 
     } 
    }; 

    // Expose test to the global object 
    window.test = test; 
}(window.jQuery, window)); 

test.fn.init.prototype = test.fn; 

Oluşturulan örneklerdeki kurucu ve prototip zincirini kaçırdınız.

+0

Ah, şimdi anlıyorum. Çok teşekkürler! – jleck

+0

Aslında bir şey daha, "constructor: test" bu durumda ne yapıyor? – jleck

+0

Aslında, bu işe yaradı. "Yeni test()" yaparsanız, kurucu olarak orijinal test işlevini kullanır. D'oh! – jleck

İlgili konular