javascript与原型js冲突

时间:2014-01-16 13:39:40

标签: javascript jquery asp.net prototypejs

我有以下代码,在我将Prototype.js包含在页面中后,这已经开始破解。

          function JsonArrayByProperty(objArray, prop, direction) {
            if (arguments.length < 2) throw new Error("sortJsonArrayByProp requires 2 arguments");
            var direct = arguments.length > 2 ? arguments[2] : 1; //Default to ascending

            if (objArray && objArray.constructor === Array) {
                var propPath = (prop.constructor === Array) ? prop : prop.split(".");
                objArray.sort(function (a, b) {
                    for (var p in propPath) {
                        if (a[propPath[p]] && b[propPath[p]]) {
                            a = a[propPath[p]];
                            b = b[propPath[p]];
                        }
                    }
                    a = a.match(/^\d+$/) ? +a : a;
                    b = b.match(/^\d+$/) ? +b : b;
                    return ((a < b) ? -1 * direct : ((a > b) ? 1 * direct : 0));
                });
            }
        }

它在以下行中出现错误

   Uncaught TypeError: Object #<Object> has no method 'match' 

   a = a.match(/^\d+$/) ? +a : a;
   b = b.match(/^\d+$/) ? +b : b;

1 个答案:

答案 0 :(得分:3)

你的问题最有可能从这一行开始:

for (var p in propPath) {

将prototype.js添加到页面后,您无法使用使用for(foo in bar)迭代数组的常用(但不正确)快捷方式。这是因为数组元素不再是简单的字符串或浮点数,它们是完全成熟的“扩展”对象,如果你正确地迭代它们,它们会碰巧回溯到字符串或浮点数。

for(var i = 0; i < propPath.length; i++) {

会让你回到正轨。

相关问题