Объясните этот запутанный синтаксис руководства по додзё для объявления

Я читаю синтаксис использования 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, {})

Возможно, это многословно, но, по крайней мере, легко сказать, для чего предназначен каждый параметр.


person Jeremy    schedule 20.11.2012    source источник


Ответы (1)


Ну, считайте, что это перегруженный конструктор:

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

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

Используйте пустой массив [] или null без superClass.

N.B.: Начиная с Dojo 1.8, нет необходимости в именованных классах, потому что dojo/parser может использовать идентификатор модуля (mid, например, "mynamespace/MyClass") для инстанцирования. Я считаю именованные классы устаревшими и противоречащими сопровождению кода.

person phusick    schedule 20.11.2012
comment
Я согласен, по возможности следует избегать необходимости в className, поскольку это делает код менее переносимым. Кроме того, className является необязательным, тогда как superClass — нет (т. е. должен быть массивом или нулевым значением). Итак, declare(null, SuperClass, {}) != класс без имени - person Stephen Simpson; 20.11.2012
comment
@phusick Я не думал о том, что он перегружен, конечно, им, вероятно, следует сказать об этом в учебнике, чтобы избежать путаницы, спасибо за ответ. - person Jeremy; 20.11.2012