[OpenLayers-Trac] [OpenLayers] #3447: Bleeding of variables

OpenLayers trac-20090302 at openlayers.org
Tue Aug 2 12:51:19 EDT 2011

#3447: Bleeding of variables
 Reporter:  jonasenander     |       Owner:  tschaub     
     Type:  bug              |      Status:  new         
 Priority:  minor            |   Milestone:  2.12 Release
Component:  BaseTypes.Class  |     Version:  2.10        
 Keywords:                   |       State:              
 Don't know if it is intentional but there seems to be a kind of obscure
 "feature"/bug when creating a "class" with OpenLayers.Class();

 When creating a new class with a init function and a variable like so:
 myObj.newClass = OpenLayers.Class({
    clsArr: [],
    clsObj: {},
    clsStr: null,
    initialize: function(){...},

 and then creating two instances of said class:
 myInst = new myObj.newClass();
 myInst2 = new myObj.newClass():

 and then adding data to the array in those instances:

 myInst.clsArr[0] = "foo";
 myInst2.clsArr[1] = "bar";

 and then inspecting said array from one of the instances:


 It will print:

 ["foo", "bar"];

 So... In this case the arrays bleed.

 Testing the other types of variables are solid tho.

 fooObj = {};
 fooObj.bar = "baz";
 myInst.clsObj = fooObj;
 >> Object {}

 myInst.clsStr = "abc";
 >> null

 It is a case of esoteric execution behavior of the prototype paradigm I

 If the array-var is instead declared inside the initialize-function:

 initialize: function(){
     this.clsArr = [];

 then it wont bleed. Im writing a revamped version of the OpenLayers.Class
 function atm, but it isnt finished yet. But I dont know if you guys are
 interested in that piece of code.

 Feature or bug? :)

Ticket URL: <http://trac.openlayers.org/ticket/3447>
OpenLayers <http://openlayers.org/>
A free AJAX map viewer

More information about the Trac mailing list