开发者

Prototype.js: How can i return an array without all the methods Prototypes extends Array with?

开发者 https://www.devze.com 2022-12-27 23:24 出处:网络
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?

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
});
0

精彩评论

暂无评论...
验证码 换一张
取 消