我第一次尝试在 java 脚本中实现鸭子类型(duck typing)以避免冗长的条件语句。下面是我的鸭子类型(duck typing)代码
// 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()
函数的字符串。
关于javascript - 在 Javascript 中实现 Duck Typing 时,.file() 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55915935/