I have some prototype functions added to Object
which in my opinion were practical in certain scenarios. However, I noticed that when I executed a $.get, the prototype functions are handled as data members and are sent like http://...?prototypefunc=false
.
This is rather useless as I don't supply these as data members, but they are added to the query string.
To be exact, I have this code:
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) { });
The corresponding URL constructed is:
http://localhost/test.php?test=foo&in=false
How can I avoid this?
jQuery runs a for...in
loop on the object passed, which iterates over all enumerable properties of an object, whether inherited or not. It doesn't do any checks to see if the object's property is owned by the object or inherited via the prototype chain.
Also, if it encounters a property whose value is a function during the serialization of the object it will execute the function and use the return value.
There are a few solutions:
Object.prototype
. .hasOwnProperty(propertyName)
. Object.defineProperty()
(ECMAScript 5 compliant browsers only). 在您的情况下,如果您不希望在函数中删除该函数,则应将数据(具有与jQuery serialize类似的功能) 序列化为字符串,并将其附加到URL。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.