這問題我想很久,但還是想不出為甚麼。
這是一個test物件,我想在建構的時候,我希望可以向JAVA,呼叫物件本身的function來設定number,
卻一直出現 Uncaught TypeError: this.setNum is not a function
at new test (<anonymous>:4:10)
at <anonymous>:1:5
如果先建立這物件,之後再呼叫的話就可正常執行。
問題主旨大概是: 為甚麼不能再建構時呼叫物件本身的function。
//下面程式碼只是隨便舉個例子
function test(str){
this.name = str;
this.number;
this.setNum();//這行出事
this.setNum = function(){
this.number = 1;
}
}
//直接執行
var t = new test("a");
// 然後就卡在this.setNum了
//假設我把this.setNum() 這段拿掉
var t = new test("a");
t.setNum();
// 正常執行:t.number = 1
|