道场 class 命名
Dojo class naming
我正在尝试了解如何通过 dojo 命名 类。我想我已经完成了教程,但我可能错过了一些东西。
是
define(["dojo/_base/declare"], function(declare){
return declare(null, {
constructor: function(name){
this.name = name;
}
});
});
在Person.js中与
相同
define(["dojo/_base/declare"], function(declare){
return declare("Person", null, {
constructor: function(name){
this.name = name;
}
});
});
在 SomeOtherFileName.js?
什么时候我们需要在文件名中表示文件名vs作为声明的参数?第二个例子会产生什么问题吗?
您应该将您的class放在定义语句中并使用绝对路径来定义它。在函数定义中,您可以随心所欲地命名它们,只要您保持顺序不变即可。如果您在代码中引用这些,则需要使用您用于函数名称的值。在调用函数之前,请务必始终使用关键字 "this"。否则不会被发现。
define{["dojo/_base/declare",
"this/is/my/path/Person",
"this/is/my/path/SomeOtherFileName"],
function(declare,
myPeopleClass,
anotherFile) {
return declare("this.is.my.path.CurrentFile", [anotherFile], {
postCreate : function() {
var value = this.anotherFunction();
//some other code
},
anotherFunction : function() {
//more code
return something;
}
});
});
class 名称(declare()
的第一个参数)不需要表示文件的路径。实际上,它是可选的。构造函数的可选名称(松散地,"class"
)存储在创建的原型中的 "declaredClass"
属性 中。它将用作创建的构造函数的全局名称。
例如:
define(["dojo/_base/declare"], function(declare){
return declare("my.firstWidget", {
constructor: function(){
console.log(this.declaredClass)
}
});
});
这将创建一个全局构造函数,my.firstWidget
,可以像这样使用它:
require(["dojo/dom", "myApp/myWidget"], function(dom){
var my = new my.firstWidget(); //note this line
my.placeAt(dom.byId('someID'));
});
但是,这是一个不好的用法,我们有道场,我们必须按照道场的方式来做,这只是为了举例。
但是,我们确实可以以声明的方式使用它,就像这样:
<div data-dojo-type="my.firstWidget"></div>
没关系。
实际上,declare()
的第一个参数在新开发中被省略了。这确保了全局命名空间不会被 classes 污染并减少名称冲突的机会。
AMD Module ID (MID)
成为通常指的 class 名称。该名称隐含在文件路径中。例如 myApp/myWidget.js
会使 MID myApp/myWidget
。如果我们省略 class name
并想使用声明方式
,那么这个 MID
就是我们可以使用的
<div data-dojo-type="myApp/myWidget"></div>
这也有效,如果我们将 class name
给 declare()
,这甚至有效
看看here道场里有很多关于declare()
的例子和信息。
我正在尝试了解如何通过 dojo 命名 类。我想我已经完成了教程,但我可能错过了一些东西。
是
define(["dojo/_base/declare"], function(declare){
return declare(null, {
constructor: function(name){
this.name = name;
}
});
});
在Person.js中与
相同define(["dojo/_base/declare"], function(declare){
return declare("Person", null, {
constructor: function(name){
this.name = name;
}
});
});
在 SomeOtherFileName.js?
什么时候我们需要在文件名中表示文件名vs作为声明的参数?第二个例子会产生什么问题吗?
您应该将您的class放在定义语句中并使用绝对路径来定义它。在函数定义中,您可以随心所欲地命名它们,只要您保持顺序不变即可。如果您在代码中引用这些,则需要使用您用于函数名称的值。在调用函数之前,请务必始终使用关键字 "this"。否则不会被发现。
define{["dojo/_base/declare",
"this/is/my/path/Person",
"this/is/my/path/SomeOtherFileName"],
function(declare,
myPeopleClass,
anotherFile) {
return declare("this.is.my.path.CurrentFile", [anotherFile], {
postCreate : function() {
var value = this.anotherFunction();
//some other code
},
anotherFunction : function() {
//more code
return something;
}
});
});
class 名称(declare()
的第一个参数)不需要表示文件的路径。实际上,它是可选的。构造函数的可选名称(松散地,"class"
)存储在创建的原型中的 "declaredClass"
属性 中。它将用作创建的构造函数的全局名称。
例如:
define(["dojo/_base/declare"], function(declare){
return declare("my.firstWidget", {
constructor: function(){
console.log(this.declaredClass)
}
});
});
这将创建一个全局构造函数,my.firstWidget
,可以像这样使用它:
require(["dojo/dom", "myApp/myWidget"], function(dom){
var my = new my.firstWidget(); //note this line
my.placeAt(dom.byId('someID'));
});
但是,这是一个不好的用法,我们有道场,我们必须按照道场的方式来做,这只是为了举例。
但是,我们确实可以以声明的方式使用它,就像这样:
<div data-dojo-type="my.firstWidget"></div>
没关系。
实际上,declare()
的第一个参数在新开发中被省略了。这确保了全局命名空间不会被 classes 污染并减少名称冲突的机会。
AMD Module ID (MID)
成为通常指的 class 名称。该名称隐含在文件路径中。例如 myApp/myWidget.js
会使 MID myApp/myWidget
。如果我们省略 class name
并想使用声明方式
MID
就是我们可以使用的
<div data-dojo-type="myApp/myWidget"></div>
这也有效,如果我们将 class name
给 declare()
看看here道场里有很多关于declare()
的例子和信息。