Learning Ajax framework-Dojo: Section 6-declaration and inheritance of the dojo class (with source code)

Source: Internet
Author: User

Dojo implements class declaration and inheritance through the dojo. Declare method. First, let's take a look at the simplest case-how to declare a class using dojo. Declare. Next, we will explain how classes implement inheritance and multi-inheritance. This section describes how to call the methods of the parent class correctly in two cases. Finally, the method for declaring static members is given.

1. Class Declaration

 

Dojo. Declare('My. classes. foo', null ,{
Protoid: 'foo ',
Method: function (){
Return "I Am a foo ";
}
}

);

The code above uses dojo. Declare to declare a class named my. classes. Foo, which has a member variable protoid and a member method. After declaring the my. classes. Foo class, you can useCreate an object instance of this class with the New Keyword.

VaR OBJ = new my. classes. Foo ();

 

 

2. class inheritance

Dojo. Declare ('My. classes. bar',My. classes. foo,{
Protoid: 'bar ',
Method: function (){
Return "I Am a bar ";
},
Barid: 'barid'
}

);

The my. classes. Bar class inherits from my. classes. Foo. it overwrites the parent class member variable protoid and member method, and declares the new member variable Barid.

 

 

3. Multi-inheritance of Classes

Dojo. Declare ('My. classes. bar ',[My. classes. Foo, my. classes. Mixin],{
Protoid: 'bar ',
Method: function (){
Return "I Am a bar ";
},
Barid: 'barid'
});

My. classes. bar class inherited from my. classes. foo and my. classes. mixin class attributes and methods, but here, the two parent classes are not equal. In fact, my. classes. bar is just my. classes. the subclass of Foo, which copies a copy of my. classes. the attributes and methods of the Mixin class. In this way, dojo. Declare achieves "Multi-inheritance.

 

 

4. Call the parent class Method

The previously defined my. classes. Bar class overwrites the method of the parent class my. classes. Foo. How can I call the method of the parent class in the subclass? The dojo Declaration class has the superclass attribute.

 

First case: when there is only one inheritance Link

We only need to modify the definition of the method in the my. classes. Bar class, as shown below:

Method: function (){
Return "I Am a bar and" +
This. constructor. superclass. method. Apply (this, arguments );

}

Return Result: "I am a bar and I am a foo ".

 

Case 2: when there is a multi-inheritance relationship

We define a new class my. classes. zot, It's my. classes. the Child class of bar. The inheritance relationship is my. classes. zot --> my. classes. bar --> my. classes. foo.

Incorrect call method:

Assume that the method of the new class my. classes. zot is as follows:

Method: function (){
Return "I Am a zot and" +
This. constructor. superclass. method. Apply (this, arguments );

}

When zot's method is called, the bar method is repeatedly called, resulting in stack overflow.

Correct call method:

The javascript class created by dojo includesInheritedTo call the method of the parent class. Modify the bar and zot methods, that is, the code after the inherited method is added is as follows:

Dojo. Declare ('My. classes. bar', my. classes. Foo ,{
Protoid: 'bar ',
Method: function (){
Return "I Am a bar" +
This. inherited ('method', arguments );
},
Barid: 'barid'
});
Dojo. Declare ('My. classes. zot ', my. classes. Bar ,{
Protoid: 'zot ',
Method: function (){
Return "I Am a zot and" +
This. inherited ('method', arguments );
},
});

 

 

5. Class Constructor

Class constructor usageInitializerAttribute declaration, for example:

Dojo. Declare ('My. classes. foo', null ,{
Initializer: Function (ARG ){
This. protoid= 'Foo initialized ';
},
Protoid: 'foo ',
Method: function (){
Return "I Am a foo ";
}
});

The constructor declared in the subclass automatically calls the constructor in the parent class. For example:

Dojo. Declare ('My. classes. bar', my. classes. Foo ,{
Initializer:Function (ARG ){
This. protoid = 'bar initialized ';
},
Protoid: 'bar ',
Method: function (){
Return "I Am a bar" +
This. inherited ('method', arguments );
},
Barid: 'barid'

});

Declaration: when creating an object instance of the my. classes. Bar class, first call the constructor of the parent class my. classes. Foo, and then call the constructor of the subclass.

 

 

6. declare static members

The members declared through dojo. Declare are instance members of the class, and the instances of each object are independent. If you want to declare in the class that static members share in all object instances of the class, you can use the following method:

Dojo. Declare ('My. classes. foo', null ,{
Initializer: function (ARG ){
This. protoid = 'foo initialized ';
},
Protoid: 'foo ',
Method: function (){
Return "I Am a foo ";
},
Statics :{
Staticint: 0,
Staticstring: "test ",
Staticfunc: function (){
......
}
}

});

 

In any language, class is the top priority and class learning cannot be ignored. At present, it can only be understood, and bloggers will continue to work hard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.