如何工作dojo类对象

时间:2013-12-06 11:17:15

标签: javascript dojo arcgis

我已经宣布了一个dojo类并且有点困惑。

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,
    });
});

我在类中创建了名为 wgs1984 的变量,并在类中引用。以下三种方法有所不同:

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

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

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

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

此调用是否在内存上创建新实例?

1 个答案:

答案 0 :(得分:2)

在第一个示例中,将在加载模块时创建一次SpatialReference。所有modules.utils实例都将指向同一个对象。

在第二种情况下,每次实例化modules.utils对象时都会创建SpatialReference。每个utils对象都有一个单独的SpatialReference。

第三种情况没有意义。我不确定结果会是什么。

第二种情况是你大部分时间都会做的事情,但是有些情况可以使用第一个例子。

编辑:

如果你想在每次调用wgs84时创建新的SpatialReferences,你需要使用一个函数。

declare("utils",[],{
   wgs84: function(){ return new SpatialReference(...);}
})


var obj = new utils();

var instance1 = obj.wgs84();
var instance2 = obj.wgs84();

instance1和instance2不是同一个对象。