构建对象,其中构造函数是字符串 [英] Build object where the constructor is a string
本文介绍了构建对象,其中构造函数是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果构造函数是字符串,有没有办法(除了使用eval()
)来构建sapui5控件?
Is there a way (except using eval()
) to build a sapui5 control if the constructor is a string?
比如我想构建一个文本控件:
For example, I want to build a text control:
var oText = new sap.m.Text({});
但部分new sap.m.Text({})
"实际上是一个字符串.我试过 eval()
:
but the part "new sap.m.Text({})
" is actually a string.
I tried eval()
:
var sObj = "new sap.m.Text({});";
var oObj = eval(sObj);
oObj.setText("Hello");
它有效.
问题是是否有另一种更安全的方法来做到这一点.
The question is if there is another, more secure way to do it.
推荐答案
基于@dfsq 的评论,您可以使用new Function
:
Based on @dfsq's comment you can use new Function
:
var fControlGenerator = function (sControlCommand) {
return new Function("return " + sControlCommand)();
};
var oControl = fControlGenerator("new sap.m.Text()");
这篇关于构建对象,其中构造函数是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文