dojo类对象的工作原理

how works dojo class object

本文关键字:工作 对象 dojo      更新时间:2024-03-28

我已经声明了一个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 })
  });

这个调用是在内存上创建新实例吗?

在第一个示例中,加载模块时将创建一次空间引用。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不是同一个对象。