gpt4 book ai didi

javascript - 执行函数时返回字符串

转载 作者:行者123 更新时间:2023-11-27 23:39:29 26 4
gpt4 key购买 nike

我有一个观点:

Ext.define('DemoApp.view.Main', {
//Some code here
...
items: [{
xtype: 'label',
text: //<- I want to run some scripts then return a string to this.
}]
})

我尝试过:

text: function() {
return 'Test';
}

但是什么也没发生。

请帮忙!

最佳答案

解决方案 1:

function getText(){
if(someVariable==someValue){
return 'text1';
} else{
return 'text2';
}
};
Ext.define('DemoApp.view.Main', {
//Some code here
...
items: [{
xtype: 'label',
text: getText()
}]
});

在这种情况下,如果您将主定义编写为单独的文件,则 getText 函数将成为全局函数。

解决方案 2:

Ext.define('DemoApp.view.Main', {
//Some code here
...
initComponent: function(){
function getText(){
if(someVariable==someValue){
return 'text1';
} else{
return 'text2';
}
};
this.items = [{
xtype: 'label',
text: getText()
}];
this.callParent(arguments);
}
});

关于javascript - 执行函数时返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825014/

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