Permalink
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (41 sloc) 2.58 KB

Namespaces

< Events | Main

Sources: core/Namespace/Scope.js, core/Namespace/Namespace.js

Sometimes it's useful to set several clazzes into one individual namespace. To do it use namespace function. First argument of the function is namespace string ('/' is default delimiter). Last argument is namespace function. Between first and last arguments you can specify companents to pass to namespace function. By default namespace function is taking 2 arguments: first - clazz, second - subnamespace.

Example:

Online working version of this example is available on plunker: http://plnkr.co/edit/bfhjMI

clazz('Animal', {});

namespace('Animals', function(clazz, namespace) {

    clazz('Cat', '/Animal', {});

    clazz('Dog', '/Animal', {});

    namespace('Cats', 'namespace', 'clazz', function(namespace, clazz) {

        clazz('Lion', '/Animals/Cat', {});

        clazz('Tiger', '/Animals/Cat', {});

        clazz('Domestic', '/Animals/Cat', {});

        namespace('Domestic', 'clazz', function(clazz) {

            clazz('SomeOrdinaryCat', '/Animals/Cats/Domestic', {});
        });
    });
});

var Animal = clazz('/Animal');

var Cat    = clazz('/Animals/Cat');
var Dog    = clazz('/Animals/Dog');

var Lion      = clazz('/Animals/Cats/Lion');
var Tiger     = clazz('/Animals/Cats/Tiger');
var Domestic = clazz('/Animals/Cats/Domestic');

var SomeOrdinaryCat = clazz('/Animals/Cats/Domestic/SomeOrdinaryCat');

document.write('Cat.__isSubclazzOf(Animal): ' + Cat.__isSubclazzOf(Animal) + '<br>');
document.write('Dog.__isSubclazzOf(Animal): ' + Dog.__isSubclazzOf(Animal) + '<br>');
document.write('Cat.__isSubclazzOf(Dog): '    + Cat.__isSubclazzOf(Dog)    + '<br>');

document.write('Lion.__isSubclazzOf(Cat): '    + Lion.__isSubclazzOf(Cat)    + '<br>');
document.write('Lion.__isSubclazzOf(Dog): '    + Lion.__isSubclazzOf(Dog)    + '<br>')
document.write('Lion.__isSubclazzOf(Animal): ' + Lion.__isSubclazzOf(Animal) + '<br>');

document.write('SomeOrdinaryCat.__isSubclazzOf(Domestic): ' + SomeOrdinaryCat.__isSubclazzOf(Domestic) + '<br>');

< Events | Main

githalytics.com alpha