2011-10-19 29 views
1

我這裏有一些JS代碼鏈接如果你打開你的JS控制檯,你會遇到這樣一小段代碼片段Javascript繼承行爲不端

var r = new TempPopupForm("xxx"); 
r.create(); 

錯誤會出現

TypeError: this.init is not a function 
刪除

這個錯誤是說這個對象沒有實現init方法。

但是,這不是true.As你可以在代碼中看到下面的init方法聲明。

TempPopupForm.prototype = new PopupForm(); 
TempPopupForm.prototype.constructor = TempPopupForm; 
TempPopupForm.superclass = PopupForm.prototype; 

function TempPopupForm(name) { 
    this.init(name); 
} 
TempPopupForm.prototype.init = function(name) { 
    TempPopupForm.superclass.init.call(this, name); 
}; 

我想繼承定義是錯誤的,但我不知道它是什麼。

BTW有一些第三方的依賴。

編輯

我是按照本文以及其中funcs中是有序的像我有。訂單實際上適用於其他類,但不適用於此類。 http://www.kevlindev.com/tutorials/javascript/inheritance/inheritance10.htm

+0

那原始IP地址的超級鏈接感覺可疑... –

+0

這是我的測試機,所以沒有域對我來說:-) – user49126

+0

你不想使用[的jsfiddle](而不是?就個人而言,我覺得它看起來很可疑 - 使其他用戶不願意跟隨鏈接或與您的問題幫助 - 和你暴露/單挑出你的試驗機作爲黑客的目標... –

回答

0

您需要重新排序的功能和實例。由於您的構造函數正在使用其自己的原型方法來初始化,它們都需要位於實例化對象的塊之上。 JavaScript提升頂級功能。

試試這個 -

function TempPopupForm(name) { 
    this.init(name); 
} 

TempPopupForm.prototype = new PopupForm(); 
TempPopupForm.prototype.constructor = TempPopupForm; 
TempPopupForm.superclass = PopupForm.prototype; 

TempPopupForm.prototype.init = function(name) { 
    TempPopupForm.superclass.init.call(this, name); 
}; 

var r = new TempPopupForm("xxx"); 
r.create(); 
+0

我重新排序像你建議,但知道它說:TempPopupForm.superclass未定義 – user49126

+0

我做編輯...不知道是否會工作或沒有。無論如何,你爲什麼這樣做?當然有一個更簡單的方法 – mattacular

+0

現在它又說:this.init不是一個函數。我正在關注這篇文章http://www.kevlindev.com/tutorials/javascript/inheritance/inheritance10.htm – user49126