【发布时间】:2019-09-18 19:41:16
【问题描述】:
我第一次尝试在 java 脚本中实现鸭子类型,以避免冗长的条件语句。下面是我的鸭子打字代码
// A simple array where we keep track of things that are filed.
filed = [];
function fileIt(thing) {
// Dynamically call the file method of whatever
// `thing` was passed in.
thing.file();
// Mark as filed
filed.push(thing);
}
function AuditForm(reportType) {
this.reportType = reportType;
}
AuditForm.prototype.file = function () {
console.log("Hello from Here!!!");
//Call Ajax here and then populate grid
}
var AuditForm = new AuditForm("AuditForm");
这就是我的称呼
fileIt("AuditForm");
使用上面的代码,我可以访问 fileIt(thing) 函数,但在thing.file(); 出现未知错误
这里有什么问题..请提出建议。
【问题讨论】:
-
你给函数传递了一个字符串,不应该是
fileIt(AuditForm); -
@George 谢谢,现在,我试图从
var formID = obj.id;传递formID仍然出错。这有什么问题?formID的值为auditform
标签: javascript jquery typescript ecmascript-6 duck-typing