.file() 方法在 Javascript 中实现 Duck Typing 时不起作用
.file() method not working while implementing Duck Typing in Javascript
我第一次尝试在 java-script 中实现 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()
函数的字符串。
我第一次尝试在 java-script 中实现 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()
函数的字符串。