[英]Make ExtJS member variable private
Ext.define("rgpd.user.Profile", {
config: {
id: -1,
role: 0,
token: '',
corps_metier: [],
},
constructor: function(config) {
this.initConfig(config);
this.callParent(arguments);
}
});
我有这个班级的定义。 我需要对此和对象值(使用getter和setters)进行全局访问,但必须从控制台无法访问成员变量(id,token,role等)。 我尝试使用私有财产,但是没有用
编辑:
从给出的例子
Ext.application({ name : 'Fiddle',
launch : function() {
Ext.Msg.alert('Fiddle', 'Welcome to Sencha Fiddle!');
}
});
Ext.define('MyWindow', (function (){
var isWindow = true;
var isPrivateProp = true;
return {
isWindow: isWindow
};
})());
var myWindow = new MyWindow();
console.log('object: ', MyWindow);
console.log('isWindow: ', myWindow.isWindow);
console.log('isPrivateProp: ', myWindow.isPrivateProp);
myWindow.isPrivateProp = false;
console.log('isPrivateProp: ', myWindow.isPrivateProp);
如果我这样做isPrivateProp值重置为false。 我希望只能使用我的getters函数访问该属性,并且我希望只能使用我的setter来更改属性值
可以与ExtJ一起使用以拥有私有成员的模式的基本示例:
Ext.define(‘MyButton’, (function(){
function privateStaticMethod() {
console.log(‘static method’);
}
function privateMethod(me) {
console.log(me.getText());
};
function privateMethod2() {
console.log(this.getText());
}
return {
extends: ‘Ext.button.Button’,
text: 'Click me',
// will be called when the button was clicked
handler: function() {
privateStaticMethod();
privateMethod(this);
privateMethod2.apply(this);
}
};
})()
);
资料来源: http : //flexblog.faratasystems.com/index.php/private-methods-in-ext-js/
编辑:转到https://fiddle.sencha.com/#view/editor
粘贴以下代码观察控制台:
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.Msg.alert('Fiddle', 'Welcome to Sencha Fiddle!');
}
});
Ext.define('MyWindow', (function (){
var isWindow = true;
var isPrivateProp = true;
return {
isWindow: function () { return isWindow }
};
})());
var myWindow = new MyWindow();
console.log('object: ', MyWindow);
console.log('isWindow: ', myWindow.isWindow);
console.log('isPrivateProp: ', myWindow.isPrivateProp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.