|
CookbookRecipe1
Point Example
Featured SourceClass("Point", {
has: {
x: {is: "ro"},
y: {is: "rw"},
},
methods: {
clear: function () {
this.x = 0;
this.setY(0);
}
}
})
Class("Point3D", {
isa: Point,
has: {
z: {}
},
after: {
clear: function () {
this.z = 0;
}
}
})
var point = new Point3D();DescriptionThis is the classic point example. In Joose classes are created using the Class() function. The function takes two parameters: The class name and the class definition which is passed as an object literal (The stuff between the outermost {}). Classes are defined using an easy to read and easy to write declarative syntax. Keywords like "has", "methods" and "isa" are used to express the different aspects of the class. Attributeshas: {
x: {is: ro},
y: {is: rw},
},The first element of the declaration of the Point class is the has-block which defines two attributes of the Point class: x and y. The block x: {is: ro} defines x as being read only. y: {is: rw} defines y as being an read/write attribute. Joose will automatically create a getter getX for x and a getter and setter setY for y. The names of the accessor methods are created using the camelCase conventions. The first letter of the attribute name is thus automatically uppercased. Methodsmethods: {
clear: function () {
this.x = 0;
this.setY(0);
}
}The methods-block defines the method clear() for the Point class. Of course, one could put many more methods in the methods-block. Because y was declared as read/write, we can use the setY method to set the y attribute. Inheritanceisa: Point, The isa-clause in the definition of Point3D defines Point3D as being a sub class of Point. A Method Modifierafter: {
clear: function () {
this.z = 0;
}
}Joose supports so-called method modifiers. after is just one of 5 method modifierst that declare methods with special behavior. The after-modifier defines methods which will be called after methods with the same name which are defined in the super class. CreditsThis cookbook entry is based on Stevan's entry for Moose. Thank you so much! |
I think you should consider changing this example.
Geometrical points do not have object identity at all, so they aren't "objects" in mainstream OOP sense; rather, they are flat values which are compared bit-by-bit.
A somewhat better example would be something involving internal state.
While you are tight we'd have to rewrite basically every introductory book on OOP then :)
I a language that doesn't have structs there is no real way to implement such flat values.
This is a random entry, I'm actually analyzing a line of code for a research project. I'm somewhat familiar with Javascript, but not Joose or Moose. Any help understanding what this line of code means would be greatly appreciated.
Cheers,
apply: function (object) {
It checks whether a role was already applied. If yes, it just bails out. Then it does something special if the role is being applied to an instance rather than a class. Please ask future questions in joose-js@googlegroups.com
Thanks for the reply!