javascript – 为declare解释这个令人困惑的dojo教程语法

我正在阅读使用dojo’s declare进行类创建的语法.描述令人困惑:

The declare function is defined in the dojo/_base/declare module. declare accepts three arguments: className, superClass, and properties.
ClassName

The className argument represents the name of the class, including the namespace, to be created. Named classes are placed within the global scope. The className can also represent the inheritance chain via the namespace.
Named Class

// Create a new class named "mynamespace.MyClass"
declare("mynamespace.MyClass", null, {

    // Custom properties and methods here

});

A class named mynamespace.MyClass is now globally available within the application.

Named classes should only be created if they will be used with the Dojo parser. All other classes should omit the className parameter.
"Anonymous" Class

// Create a scoped, anonymous class
var MyClass = declare(null, {

    // Custom properties and methods here

});

The MyClass is now only available within its given scope.
SuperClass(es)

The SuperClass argument can be null, one existing class, or an array of existing classes. If a new class inherits from more than one class, the first class in the list will be the base prototype, the rest will be considered "mixins".
Class with No Inheritance

var MyClass = declare(null, {

    // Custom properties and methods here

});

null signifies that this class has no classes to inherit from.
Class Inheriting from Another Class

var MySubClass = declare(MyClass, {

    // MySubClass now has all of MyClass's properties and methods
    // These properties and methods override parent's

});

创建非命名类和没有超类的类的语法完全相同:

var MyClass = declare(null, {
    // Custom properties and methods here  
});

我希望没有任何超类而没有任何名称的类的语法是这样的:

var MyClass = declare(null, null, {
    // Custom properties and methods here  
});

我来自一个打字的语言背景,所以也许我误解了它在JavaScript中是如何工作的.如果教程语法正确,我无法理解如何阅读代码(没有任何注释)会知道两者之间的区别.

我原以为语法是这样的:

/*class without a name:*/ declare(null, SuperClass, {})

/*class without a name or super class:*/ declare(null, null, {})

/*class with a name but no super class:*/ declare("ClassName", null, {})

也许这很冗长,但至少很容易分辨出每个参数的含义.

解决方法:

好吧,认为它是一个重载的构造函数:

// class with a name
declare(className: String, superClass: Array, classDeclaration: Object);

// class without a name
declare(superClass: Array, classDeclaration: Object);

对于没有superClass,使用空数组[]或null.

N.B.:从Dojo 1.8开始,不需要命名类,因为dojo / parser可以使用模块id(mid,例如“mynamespace / MyClass”)进行实例化.我认为命名类是过时的和代码可维护性.

相关文章

我有一个网格,可以根据更大的树结构编辑小块数据.为了更容易...
我即将开始开发一款教育性的视频游戏.我已经决定以一种我可以...
我正在使用带有Grails2.3.9的Dojo1.9.DojoNumberTextBox小部...
1.引言鉴于个人需求的转变,本系列将记录自学arcgisapiforja...
我正在阅读使用dojo’sdeclare进行类创建的语法.描述令人困惑...
我的团队由更多的java人员和JavaScript经验丰富组成.我知道这...