javascript - 在 Javascript 中实现 Duck Typing 时,.file() 方法不起作用

标签 javascript jquery typescript ecmascript-6 duck-typing

我第一次尝试在 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/

相关文章:

javascript - 表格上的双水平滚动 - JQuery

javascript - 所选下拉菜单的模糊逻辑

typescript - 作为 Prop 传递与提取缓存 Apollo 客户端 Nextjs

javascript - Cytoscape.js mapData 映射器为所有顶点返回相同的颜色

javascript - Jquery删除文本中的空格

javascript - react : Save multiple similar inputfields in one state

javascript - 如何显示错误消息,直到输入(文本)的 URL 格式正确?在 Angular js 中

jQuery 日历 - 突出显示日期

javascript - Nativescript:如何从图库获取照片列表

javascript - Angular RXJS 返回 3 个可观察流的数组