There is just one single method you need to remember for class creation:
Ovy.define. Its basic syntax is as follows:
var Person = Ovydefinename: 'Unknown'if namethisname = name;alerttext;;var aaron = 'Aaron';aaronsay"Salad"; // alert("Salad");
var Developer = Ovydefineextend: 'Person'Developer$superclasscallthis;Developer$supersaycallthis "print "+text;;
var CanSing = Ovydefinealert"I'm on the highway to hell...";var Musician = Ovydefinemixins: CanSing
In this case the Musician class will get a sing method from CanSing mixin.
But what if the Musician already has a sing method? Or you want to mix in two classes, both of which define sing? In such a cases it's good to define mixins as an object, where you assign a name to each mixin:
var Musician = Ovydefinemixins:canSing: CanSing// delegate singing operation to mixinthismixinscanSingsingcallthis;
In this case the sing method of Musician will overwrite the mixed in sing method. But you can access the original mixed in method through special mixins property.
inherits just copy all parent's own properties and methods to sub class,
inhertis has higher performance than
mixins but less features than
var Musician = Ovydefineinherits: CanSing
var Logger = Ovydefinesingleton: trueconsole.logmsg;;Logger.log'Hello';
singleton config set to true, the class will be instantiated as singleton
Static members can be defined using the
var Computer = Ovydefinestatics:instanceCount: 0// 'this' in static methods refer to the class itselfreturn brand: brand;config:brand: nullthisinitConfigconfig;// the 'self' property of an instance refers to its classthisselfinstanceCount ++;;var dellComputer = Computerfactory'Dell';var appleComputer = Computerfactory'Mac';alertappleComputergetBrand; // using the auto-generated getter to get the value of a config property. Alerts "Mac"alertComputerinstanceCount; // Alerts "2"
In some cases, it is helpful to create a nested scope to contain some private properties. The best way to do this is to pass a function instead of an object as the second parameter. This function will be called to produce the class body:
var Bar = Ovydefinevar id = 0;returnreturn ++id;;;
When using this form of Ovy.define, the function is passed tow references to its parent class and parent prototype. This can be used as an efficient way to access any parent class and parent prototype properties and functions:
var Bar = OvydefinereturnparentClasscallthis;parentcallthis arg;;