5

クラス作成にdojoの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でこれがどのように機能するかを誤解している可能性があります。チュートリアルの構文が正しければ、(コメントなしで)コードを読んでいる人が2つの違いをどのように知っているのか理解できません。

構文は次のようになると思います。

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

これは冗長かもしれませんが、少なくとも各パラメーターの目的は簡単にわかります。

4

1 に答える 1

6

まあ、それをオーバーロードされたコンストラクターと考えてください:

// 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"名前付きクラスは廃止されており、コードの保守性に反していると思います。

于 2012-11-20T12:28:29.487 に答える