From 7f6b3e81a2efec7623bbe288b62d4d9664e3bb79 Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Thu, 11 Dec 2008 17:03:10 +0100 Subject: [PATCH] Overwrite Array#concat method for handling the arguments object in Opera. --- src/lang/array.js | 19 +++++++++++++------ test/unit/array_test.js | 5 +++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/lang/array.js b/src/lang/array.js index c5de0c5..9597ac1 100644 --- a/src/lang/array.js +++ b/src/lang/array.js @@ -134,14 +134,14 @@ Array.from = $A; } function concat() { - var array = []; - for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); + var array = slice.call(this, 0), item; for (var i = 0, length = arguments.length; i < length; i++) { - if (Object.isArray(arguments[i])) { - for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) - array.push(arguments[i][j]); + item = arguments[i]; + if (Object.isArray(item) && !('callee' in item)) { + for (var j = 0, arrayLength = item.length; j < arrayLength; j++) + array.push(item[j]); } else { - array.push(arguments[i]); + array.push(item); } } return array; @@ -171,6 +171,13 @@ Array.from = $A; toJSON: toJSON }); + // fix for opera + var CONCAT_ARGUMENTS_BUGGY = (function() { + return [].concat(arguments)[0][0] !== 1; + })(1,2) + + if (CONCAT_ARGUMENTS_BUGGY) arrayProto.concat = concat; + // use native browser JS 1.6 implementation if available if (!arrayProto.indexOf) arrayProto.indexOf = indexOf; if (!arrayProto.lastIndexOf) arrayProto.lastIndexOf = lastIndexOf; diff --git a/test/unit/array_test.js b/test/unit/array_test.js index b172075..88d8fad 100644 --- a/test/unit/array_test.js +++ b/test/unit/array_test.js @@ -185,5 +185,10 @@ new Test.Unit.Runner({ this.assertEnumEqual(['a'], $w('a ')); this.assertEnumEqual(['a'], $w(' a')); this.assertEnumEqual(['a', 'b', 'c', 'd'], $w(' a b\nc\t\nd\n')); + }, + + testConcat: function(){ + var args = (function() { return [].concat(arguments) })(1, 2); + this.assertIdentical(1, args[0][0]); } }); \ No newline at end of file