【发布时间】:2020-07-17 20:47:21
【问题描述】:
当我看到这段代码时:
CustomUIHandler.prototype.createFormSession = function(formId, payload) {
function FormSession(formId, payload, common) {
this.formId = formId;
this.payload = payload;
this.common = common;
}
FormSession.prototype.getContainer = function(clientContext) {
return xmui.XmuiHandler.getContainer(clientContext);
}
FormSession.prototype.startSession = function(clientContext, actionContext) {
this.actionContext = actionContext;
this.clientContext = clientContext;
}
FormSession.prototype.promiseFormInput = function() {
return this.renderForm();
}
FormSession.prototype.renderForm = function() {
var _this = this;
var uiContainer = this.getContainer(this.clientContext);
return new Promise(function(resolve, reject) {
uiContainer.append($(
"<div>\
<input id='myText' type='text'>\ <
button id = 'continueButton' > Continue < /button>\ <
/div>"
});
$('#continueButton').click(function() {
uiContainer.empty();
var formInput = com.ts.mobile.sdk.FormInput.createFormInputSubmissionRequest({
data: document.getElementById("myText").value
});
resolve(formInput);
});
})
}
FormSession.prototype.onContinue = function(payload) {
}
FormSession.prototype.onError = function(payload) {
alert(JSON.stringify(payload));
}
FormSession.prototype.endSession = function() {
}
return new FormSession(formId, payload, this.common);
}
以上是完整的原始代码文件。
我将其重构为正确吗:
class FormSession extends CustomUIHandler {
constructor(formId, payload, common) {
super();
this.formId = formId;
this.payload = payload;
this.common = common;
}
}
createFormSession(formId, payload) {
}
【问题讨论】:
-
在第一个示例中,
FormSession不扩展CustomUIHandler。这里可能缺少代码,但那里发生的所有事情都是一个方法 (createFormSession) 大概创建了一个FormSession的实例。 -
第一段代码没有意义。
createFormSession被定义为一个什么都不做的函数:它在其本地范围内定义一个函数,但从不使用它,因此内部函数对象被垃圾回收。 -
@FelixKling,我想我不明白
createFormSession大概在 ES6 中创建FormSession的实例是什么样子的。我在 OP 中添加了更多代码。 -
正如@trincot 所说,原始代码毫无意义。你遗漏了什么吗?
-
@Barmar,第一个代码 sn-p 实际上是我正在查看的文件的第 1 到 11 行。也许这就是为什么我对这在 ES6 中的样子感到困惑。当你说它没有意义时,你能详细说明吗?顺便说一句,我添加了更多的原始代码。
标签: javascript oop ecmascript-6