Search code examples
javascriptdojoarcgis

how works dojo class object


I have declared a dojo class and confused a bit.

define(["dojo/_base/declare",
    "config/commonConfig",
    "esri/SpatialReference",
    "esri/geometry/Extent",
    "esri/geometry/Point"],
function (declare,config, SpatialReference, Extent, Point) {

    var wgs1984 = new SpatialReference({ "wkt": config.wktWgs1984 });

    return declare("modules.utils", null, {
        wgs1984: wgs1984,
    });
});

I created varibles named wgs1984 out of the class and referenced in class. Is there a difference the following three stuations:

  var wgs1984 = new SpatialReference({ "wkt": config.wktWgs1984 });

  return declare("modules.utils", null, {
            wgs1984: wgs1984,
  });
  Is this call gives same instance on memory each time?

and

 return declare("modules.utils", null, {
            wgs1984: new SpatialReference({ "wkt": config.wktWgs1984 })
  }); 
  Is this call create new instance on memory?

and

 return declare("modules.utils", null, {
            wgs1984: SpatialReference({ "wkt": config.wktWgs1984 })
  });

Is this call create new instance on memory?


Solution

  • In the first example the SpatialReference will be created once when the module is loaded. All of your instances of modules.utils will point to the same object.

    In the second case the SpatialReference is created each time you instantiate a modules.utils object. Each utils object will have a separate SpatialReference.

    Third case does not make sense. I'm not sure what the result would be.

    The second case is what you would do most of the time, but there are cases for using the first example.

    edit:

    If you want to create new SpatialReferences each time wgs84 is called you need to use a function.

    declare("utils",[],{
       wgs84: function(){ return new SpatialReference(...);}
    })
    
    
    var obj = new utils();
    
    var instance1 = obj.wgs84();
    var instance2 = obj.wgs84();
    

    instance1 and instance2 are not the same object.