Any way that you know of in JavaScript to copy an ESRI map object by value, not by reference?

I know that I can do this:

var foo = new esri.Map("foo");
var bar = foo;
foo.id; //"foo"
bar.id; //"foo"
foo.id = "oof";
bar.id; //"oof"

what I want is to make a copy of foo so that

bar.id = "bar";
foo.id; //"foo"
bar.id; //"bar"

Any ideas? So far the best I've come up with to to construct two different maps, and then copy foo's graphics, features, etc over to bar:

var foo = new esri.Map("foo");
//do some stuff to foo, adding layers etc
var bar = new esri.Map("bar");
  • Have you tried using dojo's clone? – kenbuja Jul 10 '14 at 15:37
  • dojo.clone(map); //TypeError: Cannot read property 'id' of null – Noel Jul 10 '14 at 19:39
  • Also tried: var foobar = $.extend(true, {}, map); //RangeError: Maximum call stack size exceeded – Noel Jul 10 '14 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.