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