【发布时间】:2016-01-01 03:36:15
【问题描述】:
所以我写了一个辅助函数
Object.prototype.Where = function ( boofunc ) {
// Returns an object whose own properties are
// those properties p of this object that satisify
// the condition boofunc(p)
var that = new Object();
for ( var prop in this )
{
if ( this.hasOwnProperty(prop) && boofunc(this[prop]) )
that[prop] = this[prop];
}
return that;
}
我确定这会破坏我的代码,因为它会给我诸如
之类的错误对象不支持属性或方法'exec'
在我包含的其他 JavaScript 文件中。问题是我已经使用了这个功能100次......所以我想知道是否有任何方法可以改变身体来解决问题。如果我必须摆脱此功能并将其更改为类似
function Where ( obj, boofunc )
{
var newobj = new Object();
for ( var prop in obj )
{
if ( obj.hasOwnProperty(prop) && boofunc(obj[prop]) )
newobj[prop] = obj[prop];
}
return newobj;
}
然后我必须在我的代码中经过 100 个不同的地方来更改它。
【问题讨论】:
标签: javascript