Javascript Dojo中未定义this.own
我正试图在Dojo中使用一个名为loadsth的方法创建一个singleton类。我想在foreach循环中运行this.own。然而,当我运行这段代码时,它说Javascript Dojo中未定义this.own,javascript,dojo,Javascript,Dojo,我正试图在Dojo中使用一个名为loadsth的方法创建一个singleton类。我想在foreach循环中运行this.own。然而,当我运行这段代码时,它说 TypeError: this.own is not a function 我查看了Dojo文档和脚本,其中指出方法“own”是dijit/Destroyable的一部分。但是,尽管它包括在内,但它不起作用。我在//@1和//@2两个位置试过。我实际上需要它在位置/@2,但我想确保foreach循环不会隐藏“this”。所以我尝试了/
TypeError: this.own is not a function
我查看了Dojo文档和脚本,其中指出方法“own”是dijit/Destroyable的一部分。但是,尽管它包括在内,但它不起作用。我在//@1和//@2两个位置试过。我实际上需要它在位置/@2,但我想确保foreach循环不会隐藏“this”。所以我尝试了/@1,但效果不太好
define([
"dojo/_base/declare",
"dijit/Destroyable"
], function (
declare,
destroyable
) {
var SingletonClass = declare("some.Class", null, {
_registerdPropertyGrids: [],
_resourceNode: "",
/*
* This method is used register a property grid plus
* provide the properties that should be shown in that
* grid.
*/
registerWidget: function(widgetName, propertyGridWidget) {
console.log(widgetName);
this._registerdPropertyGrids.push({widgetName, propertyGridWidget});
},
/*
* Sets the resource node for this widget.
*/
setResourceNode: function(resourceNode) {
this._resourceNode = resourceNode;
},
/*
* Loads sth.
*/
loadSth: function() {
console.log("load");
//var deferred = this.own(...)[0]; // @1
this._registerdPropertyGrids.forEach(function(element, index, array) {
console.log('_registerdPropertyGrids[' + index + '] = ' + element.widgetName);
var deferred = this.own(...)[0]; // @2
}, this);
}
});
if (!_instance) {
var _instance = new SingletonClass();
}
return _instance;
});
我想它与单个类的实现有关
因此,我的实际问题是:为什么它说当我在“define”的依赖项列表中有dijit/Destroyable时,this.own没有定义?您有
dijit/Destroyable
作为依赖项列出,但实际上您并没有使声明的构造函数从中扩展,因此,您自己的原型上没有own
您需要的不是declare(“…”,null,{
),而是declare(可销毁,{
(其中destroyable
正在替换null
)
注:
- 要声明的字符串参数已被弃用,因为它填充了全局命名空间,AMD不鼓励这样做
- 我建议将
重命名为destroyable
。构造函数的常见约定是以大写字母开头destroyable