var wrapper = function(obj) {
this._wrapped = obj;
};
我看underscore.js时候看到的,下面这段是包装_
var _ = function(obj) {
return new wrapper(obj);
};
然后我就不懂这种包装类的用途了… 求指点
5 回复
var wrapper = function ( obj ) {
this._wrapped = obj;
};
wrapper.prototype.forEach = function ( fn ) {
var i = 0 , len = this._wrapped.length;
for ( ; i < len ; i ++ ) {
fn( this._wrapped[i] );
}
};
var _ = function ( obj ) {
return new wrapper( obj );
};
_( [1, 2, 3, 4] ).forEach( function ( item ) {
console.log( item );
} );