Simple typo, which goes to show why proper indentation is ESSENTIAL:
var Invoice = function(title) {
this.title = title;
this.addItem = function addItem(item_name) {
console.log("Hello, you added " + item_name);
};
}; //you were not closing your constructor
You could, and probably should use prototype
.
var Invoice = function(title) {
this.title = title;
};
Invoice.prototype.addItem = function(item_name){
console.log("Hello, you added " + item_name);
};
For a detailed explanation of the difference, see JS - why use prototype
or in ES6, with a class :
class Invoice {
constructor(title){
this.title = title;
}
addItem(item_name) {
console.log("Hello, you added " + item_name);
}
}
And in case your invoice is a node module, and your question is made of concatenated code, don't forget to add :
module.exports = Invoice;
So you can require it anywhere.
In all those cases, invocation is done in the same way :
let invoice = new Invoice("Video games") //or var if ES5
invoice.addItem('Xbox 360');