3

I'm learning to use dojo for a project I'm about to undertake and so confess in advance this is probably a very basic question. I've tried to find the answer but without success. Also, I'm having to use a slightly older version of dojo - 1.6 I think so without AMD.

I've delcared a class using dojo.declare, here is a slightly modified example:

dojo.declare("myNamespace.CustomClass", null, {
    firstProperty: "The default value",
    constructor: function () { }
    Test: function () {
        alert(this.firstProperty);
    }
});

So, essentially, a class called CustomClass with a public property called firstProperty that has a default value of "The default value", a constructor that doesn't currently do anything and a public method called Test that when called will alert with the value of firstProperty.

My assumption was that when I create an instance of CustomClass and call Test() I would get an alert box with "The default value". However, I don't, I get an alert box with "undefined" instead. If I set the value of firstProperty after the instance has been created and then call Test, it works and I get whatever the property had been set to. A way around would therefore be to set the default values in the constructor but what I've read suggests it should work the way I assumed so I'd rather do it correctly.

I've also tried calling dojo.safeMixin(this, null) in the constructor as something I read made my wonder if this was required, but it didn't make a difference.

Thanks in advance to anyway that reads this! Simon

Kate
  • 1,556
  • 1
  • 16
  • 33

1 Answers1

5

The key concept you are missing is the concept of scope. when you do alert(firstProperty);, you are referring to a local variable firstProperty. Which, in your case you have not defined any local variable wit that name. You have, however defined an instance variable with that name. Unlike in a language such as java, instance variables must be accessed explicitly using this.

So your example should be:

dojo.declare("myNamespace.CustomClass", null, {
firstProperty: "The default value",
constructor:function(params){
    console.log(params);
    dojo.safeMixin(this,params);
},
Test: function () {
    alert(this.firstProperty);
}

});

var myInstance = new myNamespace.CustomClass({firstProperty:'something else'});
myInstance.Test();

You might also want to take a look at Javascript Scope Tricks, since scope in javascript can get pretty tricky.

EDIT: I realized you weren't inherited from dijit._Widget. In that case, you need to manually apply the constructor parameters to the instance using dojo.safeMixin. I updated the code example above. I also created a simple jsfiddle demonstrating the code.

Community
  • 1
  • 1
BuffaloBuffalo
  • 7,703
  • 4
  • 28
  • 29
  • Hi, thanks very much for the quick answer. You answered before I realised the typo I'd made in the example, which I've now ammended. In my actual code I have used 'this' in the alert function. I believe you are entirely correct that my error is in understanding scope tho, but still can't work out where I've gone wrong. – Kate Apr 02 '13 at 16:54
  • Thanks very much for your help - it's working! You mentioned in the edit about the fact I'm not inheriting from dijit._Widget. Should I be (In terms of best practice or usefulness that _Widget provides?). Again, thanks. – Kate Apr 03 '13 at 01:36
  • 1
    _Widget (and it's parent class _WidgetBase) have a bunch of helpful methods and hooks for controlling the lifecycle of the widget (beyond simple constructors). It also adds a couple of nice helper methods like automatic getter/setter support and connecting to events/method calls. Check out http://dojotoolkit.org/reference-guide/1.8/dijit/_WidgetBase.html#dijit-widgetbase and http://dojotoolkit.org/documentation/tutorials/1.6/understanding_widget/ for a little more information, – BuffaloBuffalo Apr 03 '13 at 12:10