这段代码有什么问题,因为我没有得到所需的输出? 通用对象声明-Javascript 函数myBooks(名称、作者、图书类型、打印成本、购买成本){ this.name=名称; this.author=作者; this.bookType=bookType; this.printCost=printCost; this.purchaseCost=采购成本; var checkProfit=(函数(){ 返回this.printCost-this.purchaseCost; }()); } var book1=新myBooks('Elon Musk','Ashlee Vance','传记',699500); 单据.写入(“+book1.name+”=“+book1.checkProfit”产生的利润);
您好,我已经用Javascript编写了一个基本代码,其中我使用构造函数声明了一个对象。这里,在我的代码的第20行中,book1.checkProfit得到的值是NaN,而其他变量工作正常。有人能解释我代码中的错误吗。使用that=this,这在您的支票利润窗口中这段代码有什么问题,因为我没有得到所需的输出? 通用对象声明-Javascript 函数myBooks(名称、作者、图书类型、打印成本、购买成本){ this.name=名称; this.author=作者; this.bookType=bookType; this.printCost=printCost; this.purchaseCost=采购成本; var checkProfit=(函数(){ 返回this.printCost-this.purchaseCost; }()); } var book1=新myBooks('Elon Musk','Ashlee Vance','传记',699500); 单据.写入(“+book1.name+”=“+book1.checkProfit”产生的利润);,javascript,html,Javascript,Html,您好,我已经用Javascript编写了一个基本代码,其中我使用构造函数声明了一个对象。这里,在我的代码的第20行中,book1.checkProfit得到的值是NaN,而其他变量工作正常。有人能解释我代码中的错误吗。使用that=this,这在您的支票利润窗口中 <!DOCTYPE html> <html> <head> <title>A generalized object declaration - Javascript</title&
<!DOCTYPE html>
<html>
<head>
<title>A generalized object declaration - Javascript</title>
</head>
<body>
<script type="text/javascript">
function myBooks(name,author,bookType,printCost,purchaseCost){
this.name = name;
this.author = author;
this.bookType =bookType;
this.printCost = printCost;
this.purchaseCost =purchaseCost;
var checkProfit = (function(){
return this.printCost-this.purchaseCost;
}());
}
var book1 = new myBooks('Elon Musk','Ashlee Vance','Biography',699,500);
document.write('Profit from '+ book1.name + ' = ' + book1.checkProfit);
</script>
</body>
</html>
或者你可以使用bind
这个对我来说很有用。看一看
为什么不使用:
var checkProfit=this.printCost-this.purchaseCost
?我试图学习并应用立即调用匿名函数的概念。因此,更喜欢这种方式。checkProfit
是myBooks
中的局部变量,您不能从函数外部访问它。将其定义为属性,即this.checkProfit=…
。好的。但即使我使用this.checkProfit,它仍然返回NaN。最后一个括号位于错误的位置,它应该是(function(){})(
,而不是(function{}())
尝试了这个。这对我不起作用。使用this.checkProfit,您将从外部访问函数实例的局部变量。还是不行。@AkshitaBhatt你做错了什么。Piyush.kapoor的回答(将.write
替换为.body.innerHTML
,因为JSFIDLE不接受.write
)。感谢您提供了正确的代码,但我仍然无法理解代码中的错误。谁能解释一下吗?
function myBooks(name,author,bookType,printCost,purchaseCost){
this.name = name;
this.author = author;
this.bookType =bookType;
this.printCost = printCost;
this.purchaseCost =purchaseCost;
var that = this;
this.checkProfit = (function(){
return that .printCost-that.purchaseCost;
}.bind(this)());
}
function myBooks(name,author,bookType,printCost,purchaseCost){
this.name = name;
this.author = author;
this.bookType =bookType;
this.printCost = printCost;
this.purchaseCost =purchaseCost;
var that = this;
this.checkProfit = (function(){
return that .printCost-that.purchaseCost;
}.bind(this)());
}
function myBooks(name,author,bookType,printCost,purchaseCost){
this.name = name;
this.author = author;
this.bookType =bookType;
this.printCost = printCost;
this.purchaseCost =purchaseCost;
var that =this;
this.checkProfit = (function(){
return parseInt(that.printCost)-parseInt(that.purchaseCost);
}.bind(this)());
}