dojo的私有变量 [英] dojo's private variable

查看:118
本文介绍了dojo的私有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在dojo 1.7 / 1.8中,我发现了两种定义私有变量的方式,但是两者都是他们是静态的(由类的所有实例共享)



1.使用匿名即时功能:

  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([
'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;
}
});

});

解决方案假设我正确地理解了这个问题,我不认为有什么好办法。



根据 Dojo Style Guid ,私有变量和方法应该用前面的下划线标记(例如, _myPrivateProperty _myPrivateMethod())。然而,这只是惯例,并不使它们变得私密;他们仍然可以在课外访问。



您可以创建私有静态变量,如您所说。另一种途径是在类方法中创建变量,这些变量只能在包围大括号的范围内可见(如保罗所建议的那样) Kunze 在他的回答)。然后,您可以在函数参数中将它们传递给该类。但是,我猜这不是你要找的。

可能用静态对象和每个类实例访问它自己的对象来做一些聪明的事情属性。然而,这并不常见而且复杂。我建议坚持使用强调属性的标准来标记您认为是私人的变量。


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

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

1.use 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 the private 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;
    }
});

});

解决方案

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

According to the Dojo Style Guid, private variables and methods should be marked by preceding underscore (eg. _myPrivateProperty or _myPrivateMethod()). However, this is just convention and does not make them private; they can still be accessed outside of the class.

You can create private static variables as you've already stated. The other route is to create variables within the class methods, these will only be visible within the scope of the enclosing braces (as already suggested by Paul Kunze in his answer). You could then pass them around the class in function parameters. However, I'm guessing that is not what you are looking for.

It might be possible to do something clever with a static object and each class instance accessing it's own object property. However, this would not be unusual and complicated. I'd advise sticking to the standard of using underscored-properties to mark variable that you consider private.

这篇关于dojo的私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆