我已经声明了一个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 })
});这个调用是否在内存中创建新实例?
发布于 2013-12-06 17:09:25
在第一个示例中,SpatialReference将在加载模块时创建一次。modules.utils的所有实例都将指向同一个对象。
在第二种情况下,每次实例化SpatialReference对象时都会创建modules.utils。每个utils对象都有一个单独的SpatialReference。
第三种情况没有道理。我不知道结果会是什么。
第二种情况是大多数情况下您会做什么,但是有一些使用第一个示例的情况。
编辑:
如果您想在每次调用SpatialReferences时创建新的wgs84,则需要使用一个函数。
declare("utils",[],{
wgs84: function(){ return new SpatialReference(...);}
})
var obj = new utils();
var instance1 = obj.wgs84();
var instance2 = obj.wgs84();instance1和instance2不是同一个对象。
https://stackoverflow.com/questions/20422535
复制相似问题