我已经声明了一个道场类,但有点困惑。
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 })
});
此调用是否在内存中创建新实例?
最佳答案
在第一个示例中,加载模块时将创建一次 SpatialReference。 module.utils 的所有实例都将指向同一个对象。
在第二种情况下,每次实例化 module.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 不是同一个对象。
关于javascript - Dojo 类对象如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20422535/