解释这个令人困惑的 dojo 教程语法声明

Posted

技术标签:

【中文标题】解释这个令人困惑的 dojo 教程语法声明【英文标题】:Explain this confusing dojo tutorial syntax for declare 【发布时间】:2012-11-08 11:25:18 【问题描述】:

我正在阅读使用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 中的工作原理。如果教程语法正确,我无法理解阅读代码的人(没有任何 cmets)如何知道两者之间的区别。

我本来希望语法是这样的:

/*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, )

也许这很冗长,但至少很容易分辨出每个参数的用途。

【问题讨论】:

【参考方案1】:

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

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

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

使用空数组[]null 表示没有superClass

注意:从 Dojo 1.8 开始,不需要命名类,因为 dojo/parser 可以使用模块 ID(mid,例如 "mynamespace/MyClass")进行实例化。我认为命名类已经过时并且不利于代码的可维护性。

【讨论】:

我同意,应该尽可能避免使用 className,因为这会降低代码的可移植性。此外,className 是可选的,而 superClass 不是(即必须是数组或 null)。所以,declare(null, SuperClass, ) != class without a name @phusick 没想到会超载,当然应该在教程里说,以免混淆,谢谢回复。

以上是关于解释这个令人困惑的 dojo 教程语法声明的主要内容,如果未能解决你的问题,请参考以下文章

关于 Elasticsearch json dsl 查询结构的困惑

函数表达式

Java中隐藏的方法是啥?甚至 JavaDoc 的解释也令人困惑

Dojo 1.8 图表编程教程报错

IT兄弟连 Java语法教程 三目运算符

为啥简化的 CommonJS Wrapper 语法不适用于我的 Dojo AMD 模块?