我有这段代码
function printCompanyAccount(){
var tempCompanyAccount = companyAccount();
tempCompanyAccount.printAP();
}
var companyAccount = function() {
var account = {
printAR: function() {
//Some logic
//...
//Here is when it's acting up.
console.log("Print account receivable object");
this.printAP();
},
printAP: function() {
//Business logic
//...
console.log("Print account payable object")
}
};
return account;
}
当我在printCompanyAccount()上调用tempCompanyAccount.printAP()时,它会显示
打印应收账款对象
打印应付帐款对象
我相信它应该只打印应付帐款对象,使用console.log()是不是一个坏主意,因为我的代码的多个部分都在使用它?这是一个更好的做法吗?
答案 0 :(得分:1)
如果你不做任何拼写错误,它可以正常工作。请参阅代码段:
function printCompanyAccount(){
var tempCompanyAccount = companyAccount();
tempCompanyAccount.printAP();
}
var companyAccount = function() {
var account = {
printAR: function() {
console.log("Print account receivable object");
document.body.innerHTML += "Print account receivable object";
},
printAP: function() {
console.log("Print account payable object");
document.body.innerHTML += "Print account payable object";
}
};
return account;
}
printCompanyAccount();