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);
});
}
}
因为'this' - 值的范围限定的功能,而不是对象 – adeneo
第一个'this'引用该对象之外的任何this。它不是你在想的那是 – charlietfl
而且在对象完成构建之前你不能引用一个对象的属性。 – Quentin