2012-09-10 67 views
0

我對JS中的OOP是一種新的東西。我想知道爲什麼當創建子對象時,這會停止在第二級子對象之後引用主對象。「這個」不是指當前對象

function Clase() 
{ 
    this.__construct = function() 
    { 
    this.paginator();    
    alert('__construct finished'); 
    };     

    this.paginator = function() 
    { 

     this.paginator.title = function() 
     { 
      this.paginator.title.set_offsets = function() 
      { 
       alert('paginator.title.set_offsets executed!'); 

      }; 
     }; 

     this.paginator.title(); //instantiating 

     alert('subobject paginator created');    
    }; 

    this.__construct(); 
} 

var instancia = new Clase(); 

instancia.paginator.title.set_offsets(); 

http://jsfiddle.net/WYWwE/

錯誤是:this.paginator是未定義的。

而現在,如果我用瓶蓋,它完美的作品:

function Clase() 
{ 
    self = this; 

    this.__construct = function() 
    { 
     this.paginator();    
     alert('__construct finished'); 
    };     

    this.paginator = function() 
    { 

     self.paginator.title = function() 
     { 
      self.paginator.title.set_offsets = function() 
      { 
       alert('instancia.paginator.title.set_offsets() executed'); 

      }; 
    }; 
    self.paginator.title(); 

    alert('this.paginator created'); 
}; 

this.__construct(); 
} 

var instancia = new Clase(); 

instancia.paginator.title.set_offsets(); 

http://jsfiddle.net/esjHu/

所以,據我所知之後的某一點,「這個」停止指的類「CLASE」,指的是什麼其他。如果是這樣,以這種方式使用閉包是一種很好的做法嗎?

以self = this開始課程也是正確的;從那時起只使用「自我」?例如:http://jsfiddle.net/byGRX/

+0

「這」可以改變在一個類似的問題被問到[這裏](http://stackoverflow.com/questions/3127429/javascript-this-keyword),第一個反應鏈接到一個關於這個問題的好文章。 – Bubbles

+0

謝謝,這個鏈接很有趣,但它不能回答我的問題。我仍然不知道爲什麼當我在對象層下去時這個'丟失'。 – bgusach

回答

3

當您嵌套function s時,您將失去對「原始」this的引用。爲了彌補執行以下操作:

function Clase() { 
    var that = this; 


    this.paginator = { 

     title: { 

      set_offsets: function() { 
       alert('paginator.title.set_offsets executed!'); 

      } 
     } 
    }; 
}; 

var foo = new Clase(); 

foo.paginator.title.set_offsets();​ 

http://jsfiddle.net/vd5YK/

+0

謝謝,它的工作原理。但是有可能使用構造符號來做到這一點嗎?我寧願堅持一種風格。再次感謝! – bgusach

+0

好吧,我放棄了,我回到了文字記法。 – bgusach

0

你不會失去參考this對象,這裏發生了什麼:

例如:

function Class() { 

    this.func1 = function() { 

    this.func1.func2 = function() { 
     alert('Works!'); 
    }; 

    }; 

    this.func1.func2(); 
} 

x = new Class(); 

現在,原因你得到一個錯誤,說func2不存在是因爲func2函數對象不是直到您致電func1

function Class() { 

    this.func1 = function() { 

    this.func1.func2 = function() { 
     alert('Works!'); 
    }; 

    }; 

    this.func1(); 
    this.func1.func2(); 
} 

x = new Class(); 

而現在它的工作。

編輯:

那麼,爲什麼不這項工作:

function Class() { 

    this.func1 = function() { 

     this.func1.func2 = function() { 

      this.func1.func2.func3 = function() { 
       alert('works!'); 
      }; 

      this.func1.func2.property = 5; 
     }; 

    }; 

    this.func1(); 
    this.func1.func2(); 
} 

x = new Class(); 

x.func1.func2.func3(); 

基本上,你所要做的是添加一個名爲property特性和一個名爲func3函數對象方法func2,但問題是func2在調用func1之前未構建。這是一樣的做:

function Class() { 

    this.func1 = function() { 

     this.func1.func2 = function() {}; 

    }; 

    this.func1.func2.func3 = function() { 
     alert('works!'); 
    }; 
    this.func1.func2.property = 5; 

    this.func1(); 
    this.func1.func2(); 
} 

x = new Class(); 

x.func1.func2.func3(); 

如果你想讓它通過調用func1工作,你需要先構造函數對象func2:根據你的情況下

function Class() { 

    this.func1 = function() { 

     this.func1.func2 = function() {}; 

    }; 

    this.func1(); 

    this.func1.func2.func3 = function() { 
     alert('works!'); 
    }; 
    this.func1.func2.property = 5; 

    // this.func1.func2(); 

} 

x = new Class(); 

x.func1.func2.func3(); 
alert(x.func1.func2.property); 
+0

嗨, 這也適用於我。問題出現在我想創建另一個對象的對象時。像這樣:http://jsfiddle.net/SwNPs/ 只要我用這個。我從第二級失去了對象的引用。 謝謝! – bgusach

+0

@ ikaros45我已經延長了我的回答。 – Korikulum

+0

好的解釋Korikulum,但代碼原來太亂了。我正在尋找Xander的解決方案。我真的不喜歡文字符號,但代碼更清潔簡單。 謝謝! – bgusach

相關問題