解释这个令人困惑的dojo教程语法声明 [英] Explain this confusing dojo tutorial syntax for declare

查看:186
本文介绍了解释这个令人困惑的dojo教程语法声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读使用 dojo的声明进行课堂创作的语法。描述令人困惑:

I am reading the syntax for using dojo's declare for class creation. The description is confusing:

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

});

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

The syntax is exactly the same for creating a non-named class and a class with no superclass:

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

我期望一个类的语法没有任何超类,没有任何名称如下: / p>

I expect the syntax for a class without any super class and without any name to be like this:

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

我来自一个类型化的语言背景,所以也许我误解了JavaScript在JavaScript中的工作原理。我不明白如果有人阅读代码(没有任何意见)会知道两者之间的区别,如果教程的语法是正确的。

I am coming from a typed language background, so perhaps I've misunderstood how this works in JavaScript. I fail to understand how someone reading the code (without any comments) would know the difference between the two, if the tutorials syntax is correct.

我会期望的语法要这样做:

I would have expected the syntax to be something like this:

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

也许这是冗长的,但至少很容易告诉每个参数是什么。

Maybe this is verbose, but at least it is easy to tell what each parameter is for.

推荐答案

嗯,考虑一个重载的构造函数:

Well, consider it an overloaded constructor:

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

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

使用空数组 [] null for no superClass

Use an empty array [] or null for no superClass.

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

N.B.: As of Dojo 1.8, there is no need for named classes, because dojo/parser can use a module id (mid, e.g. "mynamespace/MyClass") for instantiation. I consider named classes to be obsolete and against code maintainability.

这篇关于解释这个令人困惑的dojo教程语法声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆