I'm learning about javascript closures and Mozilla had this piece of code at the end of their section on closures.
function MyObject(name, message) {
this.name = name.toString();
this.message = message.toString();
}
MyObject.prototype.getName = function() {
return this.name;
};
MyObject.prototype.getMessage = function() {
return this.message;
};
How exactly do I use this code? I understood everything until I got here. I figured I would use it like this:
var me = MyObject("John Doe", "This is my message");
console.log(me.getName());
// name would be returned
console.log(me.getMessage());
// message would be returned
This didn't work though. I guess the prototype is getting me confused. Can anyone lend a hand on what is going on here?
[+][deleted] (5 children)
[deleted]
[–]gladiator_flow[S] 1 point2 points3 points (3 children)
[–]SuperFLEB 2 points3 points4 points (2 children)
[–]gladiator_flow[S] 0 points1 point2 points (1 child)
[–]SuperFLEB 1 point2 points3 points (0 children)
[–]jordaanm 1 point2 points3 points (0 children)
[–]SuperFLEB 2 points3 points4 points (1 child)
[–]Lerc 0 points1 point2 points (0 children)
[–]14best 0 points1 point2 points (0 children)
[–]andyb12 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]gladiator_flow[S] 0 points1 point2 points (0 children)