简体   繁体   English

将ExtJS成员变量设为私有

[英]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);
    }
});

I have this class definition. 我有这个班级的定义。 I need to have a global access to this and the object values (using getters and setters) but the member variables (id, token, role etc.) have to be unreachable from console. 我需要对此和对象值(使用getter和setters)进行全局访问,但必须从控制台无法访问成员变量(id,token,role等)。 I tried using private property but it didn't work 我尝试使用私有财产,但是没有用

edit: 编辑:

from the example given 从给出的例子

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);

if i do this isPrivateProp value is reset to false. 如果我这样做isPrivateProp值重置为false。 I want the property to be accessible ONLY using my getters function and i want to be able to change the property value ONLY using my setters 我希望只能使用我的getters函数访问该属性,并且我希望只能使用我的setter来更改属性值

A basic example of a pattern which can be applied with ExtJs to have private members: 可以与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);
    }
};

})()
);

Source: http://flexblog.faratasystems.com/index.php/private-methods-in-ext-js/ 资料来源: http : //flexblog.faratasystems.com/index.php/private-methods-in-ext-js/

Edit: Go to https://fiddle.sencha.com/#view/editor 编辑:转到https://fiddle.sencha.com/#view/editor

paste this code observe console: 粘贴以下代码观察控制台:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM