我有一個問題,我失去了參照對象ANA的屬性裏面,當一個事件$(window).resize
丟失的功能範圍的對象
內運行,我可以做的,讓範圍。
例子:
var testObject = {
init: function() {
this.divOriginal = $('.selector-tag');
this.resizeNow();
},
resizeNow: function() {
//some another code here....
//....
//..
$(window).resize(this.resizeUpdate.bind(this)); //without bind(this) the scope is window
},
resizeUpdate: function() {
console.log($(this));
var scrollWrapper = $(this)[0].divOriginal;//the only way to refer to the divOriginal
scrollWrapper.css('border','1px solid red');
}
}
testObject.init();
有訪問對象的屬性一個更清潔的方式?
請準確解釋哪些代碼正在丟失對某個屬性的引用,並向我們展示該代碼的調用方式。函數中'this'的值由函數的調用方式決定。所以,我們必須確切地知道你在哪裏失去它以及它如何被調用,所以我們可以爲如何修復提出正確的具體建議。 – jfriend00