简体   繁体   English

Prototype.js:我如何在没有原型扩展所有方法的情况下返回数组?

[英]Prototype.js: How can i return an array without all the methods Prototypes extends Array 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';

var myVariableToPassToAjax = myArray;

If I debug myVariableToPassToAjax it looks like this: 如果我调试myVariableToPassToAjax,它看起来像这样:

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: 无论如何,您似乎都没有使用Array的Array属性,因此您可以只返回一个对象:

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. 原型扩展了Array对象的原型,因此实际上中断了for(in)循环,并增加了您自己的键重叠的风险。 Start by reading why JavaScript "associative arrays" are considered harmful , and try using an Object instead of associative arrays . 首先阅读为什么将JavaScript“关联数组”视为有害的方法 ,然后尝试使用Object而不是关联数组

Sending an object via AJAX.Request is done by simply passing it as the "parameters" option: 通过AJAX.Request发送对象,只需将其作为“参数”选项传递即可:

var myObj = {};
myObj['test'] = '1';
myObj['test2'] = '2';
new Ajax.Request([url], {
    parameters: myObj
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM