javascript - 访问类内的私有(private)属性

标签 javascript extjs extjs6

为什么以下代码不起作用(ExtJS V6)?

Ext.define('Test', {
extend: 'Ext.window.Window',
xtype: 'basic-window',

config: {
    mytitle: ''
},

constructor: function (config) {
    Ext.apply(this, config);
    this.callParent(config);
},

requires: [
           'Ext.form.Panel'
       ],

height: 300,
width: 400,
scope: this, 
title: 'title: ' + this.mytitle,
autoScroll: true,
autoShow: true,
bodyPadding: 10,
html: "Lorem ipsum",
constrain: true,
});

var t = Ext.create('Test', {mytitle: 'testtitle'});
t.show();

我希望这会将窗口的标题设置为“title:testtitle”。相反,它将标题设置为“标题:未定义”。

附加组件:如果我使用

...
title: 'title' + this.getMytitle(),
...

我收到“未捕获的类型错误:this.getMytitle 不是函数”。为什么?

最佳答案

第一个问题 当计算 title: 'title: ' + this.mytitle 时,this 并不指向您的类的实例。您应该从构造函数

中执行此操作

还有callParent 的调用需要一个参数数组,始终调用 this.callParent(arguments)

会更容易

最后 您只能在调用构造函数后调用 this.getMytitle()

参见https://fiddle.sencha.com/#fiddle/uh9

constructor: function(config) {
    this.callParent(arguments);
    this.setTitle( 'title: ' + this.getMytitle() )                      
},
<小时/>

关于配置响应正在设置的配置的正确方法

通过实现updateMytitle,只要有人调用setMytitle('title'),它也会起作用

https://fiddle.sencha.com/#fiddle/uha

Ext.define('Test', {
    extend: 'Ext.window.Window',
    xtype: 'basic-window',
    requires: ['Ext.form.Panel'],
    config: {
        mytitle: ''
    },

    updateMytitle: function(mytitle) {
        this.setTitle('title: ' + mytitle);        
    },

关于javascript - 访问类内的私有(private)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32789194/

相关文章:

javascript - ajaxError() 在 $.ajax 错误处理程序之后执行

javascript - 按钮菜单内的 Ext JS 组合框不保持焦点

javascript - ExtJs 模式面板 Controller 看不到我的网格

javascript - Sencha 触摸2 : How to show a List in a Navigation View?

javascript - 如何将事件监听器添加到对象数组

sencha-cmd - Sencha Cmd 禁用压缩器

extjs 6 网格自定义标题

javascript - 使用 Javascript 通过数组循环遍历数组

javascript - 几个生成的 div 中的最后一个的 onmouseover 在 IE 中不起作用

javascript - 如何在 asp.net 中的文本框占位符中使用 mathjax?