<!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>
Hi, I have written a basic code in Javascript in which I am declaring an object using constructor function. Here, in 20th line of my code, book1.checkProfit is getting value NaN while other variables are working fine. Can anyone explain the error in my code.