2012-11-20 11 views
5

私はdojo's declareをクラス作成に使用するための構文を読んでいます。説明は紛らわしいです:宣言のためのこの混乱するdojoチュートリアルの構文を説明してください

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

多分これは冗長ですが、少なくとも各パラメータが何のためにあるのか言うのは簡単です。

答えて

6

まあ、それはオーバーロードされたコンストラクタを検討:

// 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/parserは、インスタンス化のためにモジュールID(mid、たとえば、たとえば)を使用できるため、Dojo 1.8以降、名前付きクラスは不要です。名前付きクラスは時代遅れであり、コードのメンテナンス性に反していると私は考えている。

+0

私は同意すると、classNameはコードを移植性が低いと考えられるので避けてください。また、classNameはオプションです。-asは、superClassではありません(つまり、配列またはnullでなければなりません)。したがって、宣言(null、SuperClass、{})!=名前のないクラス –

+0

http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#signature –

+0

@phusick私は考えなかったそれはもちろん、混乱を避けるためにチュートリアルでは、返信のおかげで言わなければならないでしょう。 – Jeremy