JavaScript doesn't explicitly include the concept of classes, yet the core language can be used to build object systems that work similarly to those found in languages that include native support for object-oriented programming(Java, Ruby and the rest).
Building a calss definition system is a lot of work. Fortunately, Dojo does this for you. dojo.declare defines class definition machinery, complete with single-inheritance, mixins, two-phase construction, and several other features.
Since javaScript is a dynamic language, it can generate code on the fly that can be consumed immediately.
The way using in JavaScript:
A good understanding of core JavaScript language concepts is required to use Dojo's class definition machinery effectively. Let's review a few concepts before start.
Prototypes and prototypal Inheritance.
Every object in JavaScript includes a reference to anther object termed the first object's prototype. Since the prototype is another object itself, it also contains a reference to its prototype. This forms a chain of objects. The chain terminates with the prototype for the built-in Object type. Although you can't access an object's prototype directly as if it were an ordinary property, we'll show an object's prototype as the property _PROTO_ when we draw diagrams.
When a property of an object is read. JavaScript looks for the property in the object. If not found, JavaScript then looks in the object's prototype, the protype's prototype, and so on, up the prototype chain until the property is found or the chain is exhausted.
Since a method is just a property that happens to be a function, this is how method dispatching occurs, and this system is called prototypal inheritance.
This explanation does not apply when a property is written. Whean a property is written to an object, it is automatically created if it does not already exist. Notcie that looking up a method is a read-only operation even if executing the method found results in changing the contents of the object.
An object's prototype reference is set when it is created, and there is no way to change this reference for the lifetime of the object. Since there are two ways to create objects in JavaScript, there are two ways to set an object's prototype reference. First, every object created by an object literal has its prototype reference set to the prototype of the built-in type Object:
var o1 = {};
var o2={};
var o3 = new Object();
// o1, o2 and o3 all have the same prototype object
Second, every object created by the keywork new combined with a constructor function has its prototype reference set to the prototype property of the constructor function:
var p1 = {};
function SomeConstructor() {}
SomeConstructior.prototype = p1;
SomeConstructor.prototype.constructor = SomeConstructor;
var o1 = new SomeConstructor();
var o2 = new SomeConstructor();
// o1 and o2 have the same prototype object p1
Here's the punch line:
And dojo.declare builds these prototype chains and constructor functions for us. Finally, here are a few key terms,
Building a calss definition system is a lot of work. Fortunately, Dojo does this for you. dojo.declare defines class definition machinery, complete with single-inheritance, mixins, two-phase construction, and several other features.
Since javaScript is a dynamic language, it can generate code on the fly that can be consumed immediately.
The way using in JavaScript:
A good understanding of core JavaScript language concepts is required to use Dojo's class definition machinery effectively. Let's review a few concepts before start.
Prototypes and prototypal Inheritance.
Every object in JavaScript includes a reference to anther object termed the first object's prototype. Since the prototype is another object itself, it also contains a reference to its prototype. This forms a chain of objects. The chain terminates with the prototype for the built-in Object type. Although you can't access an object's prototype directly as if it were an ordinary property, we'll show an object's prototype as the property _PROTO_ when we draw diagrams.
When a property of an object is read. JavaScript looks for the property in the object. If not found, JavaScript then looks in the object's prototype, the protype's prototype, and so on, up the prototype chain until the property is found or the chain is exhausted.
Since a method is just a property that happens to be a function, this is how method dispatching occurs, and this system is called prototypal inheritance.
This explanation does not apply when a property is written. Whean a property is written to an object, it is automatically created if it does not already exist. Notcie that looking up a method is a read-only operation even if executing the method found results in changing the contents of the object.
An object's prototype reference is set when it is created, and there is no way to change this reference for the lifetime of the object. Since there are two ways to create objects in JavaScript, there are two ways to set an object's prototype reference. First, every object created by an object literal has its prototype reference set to the prototype of the built-in type Object:
var o1 = {};
var o2={};
var o3 = new Object();
// o1, o2 and o3 all have the same prototype object
Second, every object created by the keywork new combined with a constructor function has its prototype reference set to the prototype property of the constructor function:
var p1 = {};
function SomeConstructor() {}
SomeConstructior.prototype = p1;
SomeConstructor.prototype.constructor = SomeConstructor;
var o1 = new SomeConstructor();
var o2 = new SomeConstructor();
// o1 and o2 have the same prototype object p1
Here's the punch line:
- Class hierarchies are simulated in JavaScript by building prototype chains.
- Class instances are created by constructor functions that hold references to these prototype chains in their prototype properties.
And dojo.declare builds these prototype chains and constructor functions for us. Finally, here are a few key terms,
- Object: An instance of the JavaScript type Object which is an area of memory that holds values for a set of properties
- instance: Synonym for an object
- property: A named storage location contained by an object
- method : A property that references a function
- class: An abstract notion of a set of objects that contain the same properties and define the same semantics for each contained property
- type: synonym for a class
- object space: A set of objects that was created as a result of executing some code.
Example:
dojo.declare(
//the name of the constructor function (class) created...
"Shape" ,
//this is where the superclass (if any) goes...
null,
//this object contains everything to add to the new class's prototype...
{
//default property values can go in prototype...
color: 0,
//here is the single prototype method...
setColor: function(color){
this.color= color;
}
}
);
Now that Shape is defined, we can use it:
var s = new Shape();
s.setColor(0x0000FF);
Define subclass:
dojo.declare(
"Rectangle" , //classname...
Shape, //superclass: Rectangle inherits from Shape...
//props hash...
{
//default values for l, w...
length: 0,
width: 0,
constructor: function(l, w) {
this.length= l || this.length;
this.width= w || this.width;
},
//these go in the prototype for Rectangle...
setLength: function(l) {
this.length= l;
},
setWidth: function(w) {
this.width= w;
},
area: function() {
return this.length * this.width;
}
}
Comments
Post a Comment