【问题标题】:.file() method not working while implementing Duck Typing in Javascript.file() 方法在 Javascript 中实现 Duck Typing 时不起作用
【发布时间】: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


【解决方案1】:
var auditForm = new AuditForm("AuditForm");
fileIt(auditForm);

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

【讨论】:

  • 谢谢,现在,我尝试从var formID = obj.id; 传递formID 仍然出错。这有什么问题? formID 的值为 auditform
  • 打开一个不同的问题,至少包括调试 console.log(obj.id) 的输出。
猜你喜欢
  • 2014-11-29
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 2021-05-03
相关资源
最近更新 更多