Is there a way to return a new version of an array/hash that does not contain all the methods/functions that prototype extends the array object with?
Example:
var myArray = $A();
myArray['test'] = '1';
myArray['test2'] = '2';
v开发者_如何学运维ar myVariableToPassToAjax = myArray;
If I debug myVariableToPassToAjax it looks like this:
Array
(
[test] => 1
[test2] => 2
[each] => function each(iterator, context) {
..........
..........
}
...and all the other extended array functions
);
Is there a way to solve this? :-/
Morten
You don't seem to be using the Array properties of the Array anyway, so you could just return an object instead:
var o = {
test1: '1',
test2: '2'
};
Prototype extends the Array object's prototype, so it practically breaks the for(in) loops, and increases the risk that your own keys overlap. Start by reading why JavaScript "associative arrays" are considered harmful, and try using an Object instead of associative arrays.
Sending an object via AJAX.Request is done by simply passing it as the "parameters" option:
var myObj = {};
myObj['test'] = '1';
myObj['test2'] = '2';
new Ajax.Request([url], {
parameters: myObj
});
精彩评论