Ich lese die Syntax für die Verwendung dojo's declare für die Erstellung von Klassen. Die Beschreibung ist verwirrend:Erklären Sie diese verwirrende Dojo-Tutorialsyntax für 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
});
Die Syntax ist genau das gleiche für eine nicht genannte Klasse erstellen und eine Klasse ohne übergeordnete Klasse:
var MyClass = declare(null, {
// Custom properties and methods here
});
Ich erwarte, dass die Syntax für eine Klasse ohne Superklasse und ohne Namen so zu sein:
var MyClass = declare(null, null, {
// Custom properties and methods here
});
ich aus einem typisierten Sprache Hintergrund komme, vielleicht habe ich das falsch verstanden, wie dies in JavaScript funktioniert. Ich verstehe nicht, wie jemand, der den Code liest (ohne Kommentare), den Unterschied zwischen den beiden kennt, wenn die Syntax der Tutorials korrekt ist.
würde ich die Syntax etwas wie dies zu erwarten sein:
/*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, {})
Vielleicht ist ausführlich, aber zumindest ist es leicht zu sagen, was jeder Parameter für ist.
Ich stimme zu, keine Notwendigkeit für einen Klassennamen sollte möglichst vermieden werden, da Code weniger tragbar macht. ClassName ist optional, where-as, superClass ist nicht (dh muss ein Array oder null sein). Also, deklarieren (null, SuperClass, {})! = Klasse ohne einen Namen –
http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#signature –
@phusick Ich hatte nicht darüber nachgedacht es ist natürlich überlastet, sollten sie wahrscheinlich sagen, dass im Tutorial Verwirrung zu vermeiden, danke für die Antwort. – Jeremy