Copying and extending global objects in javascript

Is there a way to copy a global object (Array, String ...) and then extend the copy prototype without affecting the original? I tried with this:

var copy=Array;
copy.prototype.test=2;

But if I check Array.prototype.testfor 2, because the Array object is passed by reference. I want to know if there is a way to make the "copy" variable behave like an array, but can be expanded without affecting the original Array object.

+5
source share
3 answers
. , , , -. , copy.prototype.test=2, , () .
+2

, , http://dean.edwards.name/weblog/2006/11/hooray/ , - . , :

// create the constructor
var Array2 = function() {
  // initialise the array
};

// inherit from Array
Array2.prototype = new Array;

// add some sugar
Array2.prototype.each = function(iterator) {
// iterate
};

- :

function Array2() {

}
Array2.prototype = new Array();

length IE . , , MyArray.prototype, Array.prototype. , .

+1

, . ,

copy.newFunction = function(pParam1) { 
      alert(pParam1);
};
0

All Articles