Mar*_*nze 11 javascript extjs private extend private-members
我做的一些研究ExtJS的论坛,关于私有方法和字段一个内部扩展类的,我无法找到任何真正的答案.
当我说一个扩展课程时,我的意思是这样的:
Ext.ux.MyExtendedClass = Ext.extend(Ext.util.Observable, {
publicVar1: 'Variable visible from outside this class',
constructor: function(config) { this.addEvents("fired"); this.listeners = config.listeners; }, // to show that I need to use the base class
publicMethod1: function() { return 'Method which can be called form everywhere'; },
publicMethod2: function() { return this.publicMethod1() + ' and ' + this.publicVar1; } // to show how to access the members from inside another member
});
Run Code Online (Sandbox Code Playgroud)
这里的问题是一切都是公开的.那么,如何在MyExtendedClass范围内添加一个新的变量o方法,该方法无法从外部访问但可以通过公共方法访问?
Mar*_*nze 23
以下示例显示了定义特权私有和公共成员的上层方式.但它还显示了如何定义私有静态成员(也称为类成员)和公共非特权成员.使用这些最后的2而不是特权的,我们将减少初始化时间,因为每次创建类的新对象时都不会解析它们:
Ext.ux.MyExtendedClass = Ext.extend(Ext.util.Observable,
(function() {
// private static fields (can access only to scope: minimum privileges).
var privStaticVar = 0;
// private static functions (can access only to scope and arguments, but we can send them the scope by param)
var privateFunc1 = function(me) { return me.name + ' -> ClassVar:' + privStaticVar; };
var privateFunc2 = function(me) { return me.publicMethod1() + ' InstanceVar:' + me.getPrivateVar(); };
return {
constructor: function(config) {
// privileged private/public members (can access to anything private and public)
var privateVar = config.v || 0;
var privInstFunc = function() { privateVar += 1; };
this.name = config.name;
this.incVariables = function(){ privInstFunc(); privStaticVar += 1; };
this.getPrivateVar = function(){ return privateVar; };
},
// public members (can access to public and private static, but not to the members defined in the constructor)
publicMethod1: function() { this.incVariables(); return privateFunc1(this); },
publicMethod2: function() { return privateFunc2(this); }
};
}())
);
function test() {
var o1 = new Ext.ux.MyExtendedClass({name: 'o1', v: 0});
var o2 = new Ext.ux.MyExtendedClass({name: 'o2', v: 10});
var s = o1.publicMethod2() + '<br>' + o1.publicMethod2() + '<br><br>' + o2.publicMethod2() + '<br>' + o2.publicMethod2();
Ext.get("output").update(s);
}Run Code Online (Sandbox Code Playgroud)
<link href="//cdnjs.cloudflare.com/ajax/libs/extjs/3.4.1-1/resources/css/ext-all.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/extjs/3.4.1-1/adapter/ext/ext-base.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/extjs/3.4.1-1/ext-all.js"></script>
<p>Click the button to instantiate 2 objects and call each object 2 times:</p>
<button onclick="test();">Test</button>
<p>You can click the button again to repeat. You'll see that the static variable keep increasing its value.</p>
<p> </p>
<div id="output"></div>Run Code Online (Sandbox Code Playgroud)
我使用类似下面的东西。
var toolbarClass = Ext.extend( Ext.Container,
{
/**
* constructor (public)
*/
constructor: function( config )
{
config = config || {};
// PRIVATE MEMBER DATA ========================================
var accountId = Ext.id( null, 'ls-accountDiv-');
// PUBLIC METHODS ========================================
this.somePublicMethod = function(){
console.log( accountId );
};
...
Run Code Online (Sandbox Code Playgroud)