Need to know Custom Widgets: Objects and Array are shared by reference

3
When creating my own widget, I came across a tricky Dojo feature. What I learned I like to share: Make sure; you are initiating an object/array attribute in the constructor. If you do not do that, it is shared by reference. Which could be nice be for caching or sharing. But If do not know that and you are using the widget multiple time in one page than it might lead to unexpected behavior. Dojo Documentation: http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#id6 Sample define(["dojo/_base/declare", "my/Foo"], function(declare, Foo){ return declare(null, { arr: [ 1, 2, 3, 4 ], // object. shared by all instances! num: 5, // non-object. not shared. str: "string", // non-object. not shared. obj: new Foo(), // object. shared by all instances! constructor: function(){ this.arr = [ 1, 2, 3, 4 ]; // per-instance object. this.obj = new Foo(); // per-instance object. } }); });
asked
0 answers