How to create objects in Dojo
How to create objects in Dojo ? How to inherit that objects in Dojo ( for example: I want to create开发者_JS百科 class A with field a and method ACK, then create class B and C, B inherits A and B inherits C ) ? How to do that ?
What you look for is the method dojo.declare(className, extends, fields)
:
Create class A with field a and method ACK:
dojo.declare("com.mycompany.myapp.A", null, {
a: "myValue", // field
ACK: function(param) { // method
// do something
}
});
After that declaration you can instantiate objects of your new class:
var x = new com.mycompany.myapp.A();
x.ACK(1);
alert(x.a);
If you don't like to use the full namespace and want more Java-like declarations:
var A = com.mycompany.myapp.A;
var x = new A();
Create class B and C, B inherits A and B inherits C:
dojo.declare("com.mycompany.myapp.C", null, {
// field list
});
dojo.declare("com.mycompany.myapp.B",
[com.mycompany.myapp.A, com.mycompany.myapp.C], // B inherits from A and C
{
// field list
});
You should definitely have a good look at the extensive documentation for dojo.declare
:
http://dojotoolkit.org/reference-guide/dojo/declare.html
Also potentially of interest, but certainly not to be confused with the above, are dojo.mixin
and dojo.extend
.
精彩评论