0
我寫了一個簡短而不完整的例子(爲了這個問題),試圖使用jQuery來總結一組圖像的寬度。我遇到一些問題,弄清楚如何處理複雜的OO javascript應用程序中的範圍。OO Javascript - 適當的範圍處理?
function imageContainer(){
this.selector = "img.inContainer";
this.width = function(){
var tmp = 0;
$(this.selector).each(function(){
// use load to preload images
$(this).load(function(){
// our 'this' pointer to the original object is long gone,
// so is it even possible to accumulate a sum without using
// globals? Ideally, I'd like to increment a temporary value
// that exists within the scope of this.width();
tmp+=$(this).width();
});
});
// I'm thinking that returning here is problematic, because our
// call to each() may not be complete?
return tmp;
}
this.construct = function(){
alert(this.width());
}
this.construct();
}
我真的不想找解決此問題一個黑客,我想知道如何這樣的事情應該被做 - 在不垃圾的封裝方式。我錯過了明顯的東西嗎?
非常感謝。
非常感謝,這都是非常好的信息。 – user688216 2011-04-16 01:37:04