dojo declare & private instance members

641
2
Jump to solution
03-03-2014 05:11 AM
BrianRassier
Occasional Contributor
Hello -

In looking into dojo modules, I'm simply trying to define a private instance member, and then eventually some get/set properties around them.  This is very easy to do with traditional JS object definitions.  In my research into dojo modules (via declare), this doesn't seem to be so simple.  I've seen that they can be identified as private via the leading "_" naming convention, but this doesn't actually make them private.  I've also seen how a private object can be declared within a module.  This essentially gives a private container for other private items, which is closer but not quite a simple private member.

I wanted to know if I'm missing something, or if this is just how dojo modules work?

Thanks
-Brian
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor
That's actually how JavaScript prototypes work, not necessarily special to Dojo's declare method.

Anything added to the prototype is not really hidden. The underscore is more of a convention, not a rule. The same can be said for other languages like Python, there is no real private.

What you may be looking for is the revealing module pattern, which Dojo modules can implement very effectively.

So for example, something that may seem quite Java'ish.
define(['dojo/_base/declare'], function(declare) {   var _myValue;      return declare([], {     constructor: function() {       _myValue = 1;     },     setMyValue: function(value) {       if (_myValue !== value) {         _myValue = value;       }     },     getMyValue: function() {       return _myValue;     }   }); });


So now, you have a variable _myValue that is protected inside the declared module and inaccessible outside this module, since it is scoped in the define method. You could add any checks/transformations as required in the getter/setters and feel confident that the _myValue cannot be changed unless it's through the setter.

View solution in original post

0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor
That's actually how JavaScript prototypes work, not necessarily special to Dojo's declare method.

Anything added to the prototype is not really hidden. The underscore is more of a convention, not a rule. The same can be said for other languages like Python, there is no real private.

What you may be looking for is the revealing module pattern, which Dojo modules can implement very effectively.

So for example, something that may seem quite Java'ish.
define(['dojo/_base/declare'], function(declare) {   var _myValue;      return declare([], {     constructor: function() {       _myValue = 1;     },     setMyValue: function(value) {       if (_myValue !== value) {         _myValue = value;       }     },     getMyValue: function() {       return _myValue;     }   }); });


So now, you have a variable _myValue that is protected inside the declared module and inaccessible outside this module, since it is scoped in the define method. You could add any checks/transformations as required in the getter/setters and feel confident that the _myValue cannot be changed unless it's through the setter.
0 Kudos
BrianRassier
Occasional Contributor
That explanation helps a lot.  Thanks!
0 Kudos