GibboK
GibboK

Reputation: 73938

What is the use of optional className parameter in DOJO declare?

What is the use of className in declare(className,superclass,props) in DOJO.

In the following example I am trying to use className when using in-heritage.

http://jsfiddle.net/mosnpm27/

When passing className I have receive an error.

declare(className,superclass,props);

className Optional
The optional name of the constructor (loosely, a "class") stored in the "declaredClass" property in the created prototype. It will be used as a global name for a created constructor.

require(["dojo/_base/declare"], function(declare) {
    var Mammal = declare('Mammal',null, {
        constructor: function(name) {
            this.name = name;  
        },
        sayName: function() {
            console.log(this.name);
        }
    });
    var Dog = declare('Dog', Mammal, {
        makeNoise: function() {
            console.log("Waf waf");
        }
    });

    var myDog = new Dog("Pluto");
    myDog.sayName();
    myDog.makeNoise();

    console.log("Dog: " + myDog.isInstanceOf(Dog));
    console.log("Mammal: " + myDog.isInstanceOf(Mammal));
});

Upvotes: 0

Views: 39

Answers (1)

Frode
Frode

Reputation: 5710

I'm not sure what error you receive, but the className parameter is essentially there just for legacy reasons. The declared class is placed in a global variable with that name, but when you are using AMD, you don't really need that.

For example, if you had done this:

var Dog = declare('MyLibrary.Doggie', Mammal, {
    makeNoise: function() {
        loglog("Waf waf"); //console.log("Waf waf");
    }
});

A global object called MyLibrary would have been created, containing a member named Doggie. So afterwards, you could write:

var myDog = new MyLibrary.Doggie("Pluto"); // instead of Dog("Pluto");
myDog.sayName();
myDog.makeNoise();

I don't think there is any reason at all to do this nowadays though, so you should just ignore the className parameter.

var Mammal = declare(null, { .... });
var Dog = declare(Mammal, { .... });

Upvotes: 2

Related Questions