Skip to content

API Documentation (Basic)

rodyhaddad edited this page Dec 2, 2012 · 4 revisions

Currently, ClassicalJS offers only 1 Object, the Class Object, which allows you to build and use your classes.

Here is the basic API Documentation. Enough to get you started.

#Class

##Static Methods

###>Class.Config(configObj)

Class.Config allows you to modify the configuration of ClassicalJS to suit your needs. You just need to pass a configObj, that will be merged with the existing configuration.
Example:

Class.Config({
    globalize: false
})

For a full documentation about the available configuration options, please visit the configuration page. (TODO: link to the config page)

###>Class.PersistentPlugins(pluginsObject)

Class.PersistentPlugins allows you to add plugins to all the properties and methods of your choosing, using the same format as the one used in ._().

Example:

Class.PersistentPlugins({
    PrivateVar: {
        makeGetterSetter: true // now the makeGetterSetter plugin will be applied to all private variables
    }
})

Doing this could be very useful if you want to support IE<=8.
For a full documentation on plugins and persistent plugins, please visit the plugins page. (TODO: link to the plugins page)

###>Class.addPlugin(pluginInfo)

Class.addPlugin is used for adding plugins to the library. For a full documentation on plugins authoring, please visit the plugins authoring page. (TODO: link to the plugins authoring page)

##Instance Methods

###>Class([name, classConfig])

To start defining a new Class, you need to call Class(). It will return a new Class instance, which you'll have to define in the following lines.

You can also pass a name to the Class you're creating, which is mostly useful for debugging and internal reference of Classes (if the config keepDefinedClasses is set to true).

You also have the option of passing an object as the 2nd argument, that will be given to the instance's .Config(), an easy way to immediately setting a Class's configuration.

Example:

var Person = 
    Class("Person", { superName: "parent" }) //both arguments are optional
        ...
        ...
        ...
    End()

###>.Config(configObj)

.Config allows you to change the configurations of the Class that you're currently defining.
It works the same as Class.Config, the only difference is that the new configurations will only be applied on the Class being defined right now, and not on the whole library.

Example:

var Person = 
    Class("Person")
        Config({
            superName: "parent" 
        })
        ...
        ...
    End()

###>.Constructor(fn)

.Constructor allows you to register the constructor of you class. The function you pass to .Constructor will be called when a new instance of your Class is created.

Note that if a Class doesn't have a Constructor, it's Parent Class's Constructor will be invoked instead.

Example:

var Person = 
    Class("Person")

        Public("name") // public property "name"

        Constructor(function(name){
            this.name = name;
        })
        ...
        ...
    End()

var me = new Person("Rody");
console.log(me.name); // "Rody"

###>.Extends(mixed)

.Extends allows you to specify which Class the current Class should extend.

All you have to do is pass the Class object you wish to extend.

/*in Person.js*/
var Person =
    Class("Person")
        ...
    End()

/*in Programmer.js*/
var Programmer =
    Class("Programmer").Extends(Person)
        ...
    End()

You can also pass the name of the Class you wish to extend, but to do that, keepDefinedClasses need to be set to true in the configuration option.

Example:

/*in Person.js*/
var Person =
    Class("Person")
        ...
    End()

/*in Programmer.js*/
var Programmer =
    Class("Programmer").Extends("Person") //make sure keepDefinedClasses is set to true in the config
        ...
    End()

###>.Public(...)

.Public allows you to add public properties and methods to your class.

Example:

var Person = 
    Class("Person")
        ...

        Public("firstName") //this property can be accessed and modified by everyone
        
        Private("gender")

        Public(function getGender(){ //this method can be accessed and called by everyone
            return this.gender;
        })
        ...
    End()

You can pass multiple properties/methods to .Public at the same time. To learn more, visit this page (TODO: link to the page on multiple arguments).

###>.Protected(...)

.Protected allows you to add protected properties and methods to your class.

Example:

var Person = 
    Class("Person")
        ...
        Protected("allergies") //this property can only be accessed and modified by this Class and its Sub-Classes
        
        Protected(function addAllergy(anAllergy){ //this method can only be accessed and called by this Class and its Sub-Classes
            this.allergies.push(anAllergy);
        })
        ...
    End()

You can pass multiple properties/methods to .Protected at the same time. To learn more, visit this page (TODO: link to the page on multiple arguments).

###>.Private(...)

.Private allows you to add protected properties and methods to your class.

Example:

var Person = 
    Class("Person")
        ...
        Private("feetSmells") //this property can only be accessed and modified by this Class
        
        Private(function doesFeetSmells(){ //this method can only be accessed and called by this Class
            return this.feetSmells;
        })
        ...
    End()

You can pass multiple properties/methods to .Private at the same time. To learn more, visit this page (TODO: link to the page on multiple arguments).

###>.End()

.End is used to specify that a Class definition has ended.
Note that you can't start defining a new Class, if you haven't called .End() on the last one.

Example:

var Person = 
    Class("Person")
        ...
        ...
        ...
    End()

###>.PersistentPlugins(pluginsObject)

.PersistentPlugins allows you to add plugins to all the properties and methods of your choosing in the Class you're defining, using the same format as the one used in ._().

Example:

var Person = 
    Class("Person")
        PersistentPlugins({
            PrivateVar: {
                makeGetterSetter: true // now the makeGetterSetter plugin will be applied to all private variables of this Class
            }
        })
        ...
        ...
    End()

Doing this could be very useful if you want to support IE<=8.
For a full documentation on plugins and persistent plugins, please visit the plugins page. (TODO: link to the plugins page)

###>._([position, ]pluginsObject)

._ allows you to add plugins to a specific property or method

Example:

var Person = 
    Class("Person")
        ...
        Private("gender")
            _({ type: "String", makeGetterSetter: true }) //the type plugin and makeGetterSetter plugin will be activated for the property "gender"
        ...
    End()

For a full documentation on plugins, please visit the plugins page. (TODO: link to the plugins page)