Dojo private variable

Is there a good way to define a real personal variable in dojo?

In dojo 1.7 / 1.8, I found 2 ways to define a private variable, but both of them are static private (shared by all instances of the class)

1. Use an anonymous immediate function:

define([
'dojo/_base/declare'], function(declare) {  
'use strict';    

return declare('test.Class2', null, (function(){
    var a = 1;
    return {
        constructor: function(){
            console.log('constructor');
        },
        geta: function(){
            return a;
        },
        seta: function(v){
            a = v;
        }
    };
})());

});

2. Define a personal variable in the module definition.

define([
'dojo/_base/declare'], function(declare) {  
'use strict';    
var a = 1;
return declare('test.Class1', null, {
    constructor: function(){
        console.log('constructor');
    },
    geta: function(){
        return a;
    },
    seta: function(v){
        a = v;
    }
});

});

+3
source share
4 answers

Assuming I understand the question correctly, I don't think there are good ways.

Dojo Style Guid (, _myPrivateProperty _myPrivateMethod()). ; .

, . - , ( Paul Kunze ). . , , , .

, - , . . underscored-properties, , .

+3

dojo , , .

define(["dojo/_base/declare"], function(declare){
  var _MyPrivateItem = declare(null, {
    someProperty: null
  });

  return declare(null, {
    item: null,

    constructor: function(){
      this.item = new _MyPRivateItem();
    }
  });
});

http://dojotoolkit.org/reference-guide/1.9/dojo/_base/declare.html#dojo-base-declare

, , , .

+1

?

    [...]
    return declare('test.Class1', null, {
       constructor: function(){
          console.log('constructor');
          var = privVar1 = 'foo';  // this is how its normally done in JavaScript
       },

       [..]

       //so called privileged Method is "public" but can access the "private var"
       modPrivVar1: function(){
          privVar1 = 'bar';
       }
    });

, , dojo. , .

0
source

You can include private variables ... but EVERYTHING should be inside the constructor.

[...]
return declare('test.Class1', null, {
   constructor: function(){
      console.log('constructor');
      var privVar1 = 'foo';  // this is how its normally done in JavaScript

      this.modPrivVar1 = function(){
         privVar1 = 'bar';
      }
   },
});

The disadvantage of this approach is the need to explicitly execute any inheritance that limits the value of the declare function using the dojo loader.

0
source

Source: https://habr.com/ru/post/1529428/


All Articles