gpt4 book ai didi

javascript - 使用 ExtJS 扩展类时的私有(private)成员

转载 作者:可可西里 更新时间:2023-11-01 02:15:41 26 4
gpt4 key购买 nike

我在 ExtJS 论坛上做了一些关于扩展类中的私有(private)方法和字段的研究,我找不到任何真正的答案。

当我说扩展类时,我的意思是这样的:

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

这里的问题是一切都是公开的。那么,如何在 MyExtendedClass 范围内添加一个新的变量 o 方法,不能从外部访问但可以通过公共(public)方法访问?

最佳答案

以下示例显示了 Upper Stage way定义特权私有(private)和公共(public)成员。但它也展示了如何定义私有(private)静态成员(也称为类成员)和公共(public)非特权成员。使用最后两个而不是特权对象,我们将减少初始化时间,因为它们不会在您每次创建类的新对象时都被解析:

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);
}
<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>&nbsp;</p>
<div id="output"></div>

关于javascript - 使用 ExtJS 扩展类时的私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com