For whatever reason I’m getting TypeError: Object #<Num> has no method 'getNumber' when creating numberOne as an instance of Num
function Num(n) {
var number = n;
var getNumber = function() {
return number;
};
}
var numberOne = new Num(5);
console.log(numberOne.getNumber());
You are creating a local variable called
getNumberwithout attaching it to the object. Either give it to the object, or put it on the prototype:or
If you are creating lots of objects, you probably want to put
getNumberon the prototype so it doesn’t get added to the object every single time one gets created.