2
votes

I have a serious problem about a method. So this is my method :

Object.prototype.clonage = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clonage();
    } else newObj[i] = this[i]
  } return newObj;
}

And the browser is giving me:

Uncaught RangeError: Maximum call stack size exceeded

At the line:

for (i in this) {

Can someone has the same problem?

1
You're probably calling .clonage infinitely or just really really often.Halcyon
Frits is probably right. Can you give us some context as to how you're calling it? I just tested it on a single clonage call and it worked fine.Clay Garrett
I'm using it like that : window.tweetOrberByPassed = window.displayedTweetListing.clonage(); both of them are object. I have the same function in another script page and I have no problem about it.Simon

1 Answers

1
votes

I can make javascript objects that can break your clonage function if that is an achievement of any kind :).

check: http://jsfiddle.net/Bd6XL/2/

var x = { 
    a: 5, 
    b: "asdf" 
};
var y = { 
    a: 5, 
    b: "asdf" 
};

x.y = y;
y.x = x;

Clone any of them. Yes it wont work because of circular references. Try to debug your object and see if there is any circular reference.

Also try limit your clone to what you really need.

EDIT:

Check out this question about cloning: What is the most efficient way to deep clone an object in JavaScript?

There are quite a few answers. Try the accepted one if you use jQuery:

// Shallow copy
var newObject = jQuery.extend({}, oldObject);

// Deep copy
var newObject = jQuery.extend(true, {}, oldObject);