我正在尝试第一次在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();处出现未知错误
这是怎么了..建议。

最佳答案

var auditForm = new AuditForm("AuditForm");
fileIt(auditForm);


您正在将字符串传递给fileIt而不是具有file()函数的字符串。

10-08 16:37