我有一些原型功能添加到Object
,在我看来在某些情况下是实用的。不过,我注意到当我执行$ .get时,原型函数被作为数据成员处理,并且被发送为http://...?prototypefunc=false
。
这是无用的,因为我不提供这些作为数据成员,但他们是被添加到查询字符串。
确切的说,我有这样的代码:
Object.prototype.in = function() {
for(var i=0; i<arguments.length; i++)
if(arguments[i] == this) return true;
return false;
}
$.get('http://localhost/test.php', {'test': 'foo'}, function(text) { });
相应的URL结构是:
http://localhost/test.php?test=foo&in=false
我怎样才能避免这种情况?
这就是为什么不扩展Object.prototype的原因。 – 2011-01-29 23:18:45
为什么不能?它在例如`foo.in(1,2,3)`来检查foo是否等于1,2或3. – pimvdb 2011-01-29 23:19:25
@pimvdb:因为它可能有副作用(如你所见)。 `Helper.in(this,[1,2,3])``不是更多的代码。否则,解决您的问题的唯一方法是编辑jQuery代码。 – 2011-01-29 23:21:25