javascript 私有函数和变量 ExtJs4?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5985528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:02:47  来源:igfitidea点击:

Private functions and Variables ExtJs4?

javascriptextjsprivate-membersextjs4

提问by shane87

In my current project I am using ExtJs3.3.
I have created many classes which had private variables and functions. For eg:

在我当前的项目中,我使用的是 ExtJs3.3。
我创建了许多具有私有变量和函数的类。例如:

MyPanel = function(config){
  config = config || {};

  var bar = 'bar';//private variable

  function getBar(){//public function
     return bar;
  }

  function foo(){
     //private function
  }

Ext.apply(config, {
  title: 'Panel',
  layout: 'border',
  id: 'myPanel',
  closable: 'true',
  items: []
});

  MyPanel.superclass.constructor.call(this, config);
};
Ext.extend(MyPanel , Ext.Panel, {
  bar: getBar
});
Ext.reg('MyPanel', MyPanel);

I understand that the new way of doing things in ExtJs4 is to use the Ext.definemethod. So as my above piece of code would look something like this:

我知道在 ExtJs4 中做事的新方法是使用Ext.define方法。所以正如我上面的一段代码看起来像这样:

Ext.define('MyPanel', {
  extend: 'Ext.panel.Panel',

  title: 'Panel',
  layout: 'border',
  closable: true,

  constructor: function(config) {

     this.callParent(arguments);
  },

});

So what I want to know is how can I define private variables and functions in ExtJs4 similar to the way I done it in ExtJs3?
In other words I understand that the Ext.definemethod will take care of defining, extending and registering my new class, but where should I declare javascript var's which are not properties of the class itself but are needed by the class.

所以我想知道的是如何在 ExtJs4 中定义私有变量和函数,类似于我在 ExtJs3 中的定义?
换句话说,我知道该Ext.define方法将负责定义、扩展和注册我的新类,但是我应该在哪里声明 javascript var,它们不是类本身的属性,而是类需要的。

MyPanel = function(config){
  //In my Ext3.3 examples I was able to declare any javascript functions and vars here.
  //In what way should I accomplish this in ExtJs4.

  var store = new Ext.data.Store();

  function foo(){
  }
  MyPanel.superclass.constructor.call(this, config);
};

采纳答案by Rob Boerman

I am not a big fan of enforcing private variables like this but of course it can be done. Just setup a accessor function (closure) to the variable in your constructor/initComponent function:

我不喜欢强制执行这样的私有变量,但当然可以做到。只需在构造函数/initComponent 函数中为变量设置一个访问器函数(闭包):

constructor: function(config) {
    var bar = 4;
    this.callParent(arguments);

    this.getBar = function() {
        return bar;
    }
},...

回答by VoidMain

Thats exactly what config is for, check this from Extjs docs:

这正是配置的用途,请从 Extjs 文档中查看:

config: Object List of configuration options with their default values, for which automatically accessor methods are generated. For example:

config: Object 带有默认值的配置选项列表,为其自动生成访问器方法。例如:

Ext.define('SmartPhone', {
     config: {
         hasTouchScreen: false,
         operatingSystem: 'Other',
         price: 500
     },
     constructor: function(cfg) {
         this.initConfig(cfg);
     }
});

var iPhone = new SmartPhone({
     hasTouchScreen: true,
     operatingSystem: 'iOS'
});

iPhone.getPrice(); // 500;
iPhone.getOperatingSystem(); // 'iOS'
iPhone.getHasTouchScreen(); // true;
iPhone.hasTouchScreen(); // true

That way you hide your actual field and still have access to it.

这样您就可以隐藏您的实际字段,并且仍然可以访问它。

回答by NKurapati

You can create private members like this. But it won't useful if you are making more than one instance for this class.

您可以像这样创建私人成员。但是如果你为这个类创建多个实例,它就没有用了。

Ext.define('MyPanel', function(){

    var bar = 'bar';//private variable

    function foo(){
        //private function
    };
    return {
       extend: 'Ext.panel.Panel',
       title: 'Panel',
       layout: 'border',
       closable: true,

       constructor: function(config) {

           this.callParent(arguments);
       },

       getBar: function(){//public function
           return bar;
       }

    };

});

thank you,

谢谢,

Nandu

南都