【发布时间】:2013-08-22 15:04:38
【问题描述】:
我有以下代码,它采用单个图像并对其应用特定宽度:
function Foo ( img ) {
this.image = img;
}
Foo.prototype._getWidth = function( ) {
return this.image.data('largest') + 'px';
};
Foo.prototype.applyWidth = function( ) {
this.image.css( 'width', this._getWidth() );
};
var img = Foo( $('img') );
img.applyWidth();
但是,我正在努力处理 jQuery 图像集合,例如没有 for 循环的 $('img') 或每个函数内部的 $.each()(我有超过上面显示的这两个函数)。
到目前为止,我想出的最好的是:
var temp = [];
function Create ( imgs ) {
$.each( imgs, function( i ){
temp[ i ] = new Foo ( $( this ) );
});
return temp;
}
Create( $('img') );
$.each( temp, function() {
$(this).applyWidth();
}):
这很好用,但感觉没有条理,感觉马虎。
最后,我想要一些关于以下方面的指导。
理想情况下,我希望它位于命名空间
Theme下。我想使用模块模式在Theme.Images下使用这种方法。这可能吗?
1234563
_getWidth() 的唯一值。目前我认为我需要循环 Theme.Images.temp 并在循环内调用 applyWidth()。
我真的开始欣赏 javascript 中的继承这一点,并希望继续使用它。
【问题讨论】:
标签: javascript jquery object