.file()方法在Javascript中实现Duck类型时不起作用
我第一次尝试在java脚本中实现duck类型,以避免冗长的条件语句。下面是我的鸭子打字代码.file()方法在Javascript中实现Duck类型时不起作用,javascript,jquery,typescript,ecmascript-6,duck-typing,Javascript,Jquery,Typescript,Ecmascript 6,Duck Typing,我第一次尝试在java脚本中实现duck类型,以避免冗长的条件语句。下面是我的鸭子打字代码 // 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 fi
// 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()
函数的内容
您正在将字符串传递给fileIt
,而不是具有file()
函数的内容 您正在向函数传递一个字符串,它不应该是fileIt(AuditForm)
@George谢谢,现在,我正在尝试从var formID=obj.id传递formID
代码>仍有错误。这一个有什么问题formID
的值为auditform
您正在向函数传递一个字符串,如果它不是fileIt(auditform)
@George谢谢,现在,我正在尝试从var formID=obj.id传递formID
代码>仍有错误。这一个有什么问题formID
的值为auditform
谢谢,现在,我正在尝试从var formID=obj.id传递formID
代码>仍有错误。这一个有什么问题formID
的值为auditform
至少打开一个不同的问题,包括调试console.log(obj.id)的输出。谢谢,现在,我正在尝试从var formID=obj.id传递formID
代码>仍有错误。这一个有什么问题formID
的值为auditform
至少打开一个不同的问题,包括console.log(obj.id)的调试输出。