I have this code:
function user(name) {
console.log(name);
}
user.prototype.test = function() {
return 2 + 2;
};
console.log(user.prototype.test());
var dany = new user("dany");
var david = new user("david");
console.log(dany.prototype.test());
Console logs:
4
dany
david
Uncaught TypeError: Cannot call method 'test' of undefined
Shouldn't the test() function be assigned to all instances of the user() function (which is the object constructor) ?
If you happen to have good recommendations on what I should read to understand prototype more, please go ahead too ;)
Edit:
Even using:
Object.prototype.test = function() {
return 2 + 2;
};
I still get that error in the console. I thought that all objects would inherit that prototype function too.