2016-03-04 51 views
0

如果我在這裏登錄頂級屬性,我得到一個對象,但屬性2的屬性聲明拋出一個錯誤,說該屬性是未定義的。爲什麼是這樣?如何將這些選擇器存儲在對象的頂部,以便我可以在該對象的方法中使用?爲什麼在同一對象的另一個屬性中引用對象的一個​​屬性中的jQuery選擇器會引發錯誤?

objectName = { 
     property: $('#element'), 
     property2: this.property.children('.filter-class'), 

     myFunction: function() { 
      var property2 = this.property2; 

      property2.on('click', function() { 
       property2.addClass(className); 
      }); 
     } 
    } 
+1

因爲'this' - 值的範圍限定的功能,而不是對象 – adeneo

+0

第一個'this'引用該對象之外的任何this。它不是你在想的那是 – charlietfl

+2

而且在對象完成構建之前你不能引用一個對象的屬性。 – Quentin

回答

1

如指出,它應該是:

objectName = { 
    property: $('#element'), 
    property2: $('#element').children('.filter-class'), 

    myFunction: function() { 
     var property2 = this.property2; 

     property2.on('click', function() { 
      property2.addClass(className); 
     }); 
    } 
} 
在聲明property2 this

是指窗口對象不objectName

+0

:謝謝指出 – G3z

相關問題