tags:

views:

76

answers:

2

Hi, is there a way to copy a global object (Array,String...) and then extend the prototype of the copy without affecting the original one? I've tried with this:

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

But if i check Array.prototype.test it's 2 because the Array object is passed by reference. I want to know if there's a way to make the "copy" variable behave like an array but that can be extended without affecting the original Array object.

+1  A: 

Good question. I have a feeling you might have to write a wrapper class for this. What you're essentially doing with copy.prototype.test=2 is setting a class prototype which will (of course) be visible for all instances of that class.

Codesleuth
Have you got some example for the wrapper class?
mck89
@mck89: sorry, I hadn't noticed your comment on here. S.O.'s notice feature needs some work, lol. I take it you managed to get a wrapper class sorted?
Codesleuth
Yeah i've found something on http://dean.edwards.name/weblog/2006/11/hooray/
mck89
A: 

Instead of extending the prototype, why don't you simply extend the copy variable. For example, adding a function

copy.newFunction = function(pParam1) { 
      alert(pParam1);
};
MLefrancois
Because in this way if i create a new instance of copy it won't have the method because it will take only prototype methods. Anyway this doesn't solve the problem because it extends the original Array object too.
mck89