2 ms·
It felt like the author was using the post as a cathartic persuasion for why prototypes differ from classes. JavaScript functions are a prototype of a class. Pr
by colept 13y ago
It felt like the author was using the post as a cathartic persuasion for why prototypes differ from classes. JavaScript functions are a prototype of a class. Prototyping in JavaScript is an analog to class extensions, with elaborate definitions for private/protected variables. It's a debate over semantics and syntax, and it's useless because the analog to a object oriented class can be done with JavaScript's syntax.
function MyClass()
{
var self = this;
var privateVar = 0;
this.getPrivate = function() { return privateVar; }
this.setPrivate = function(val) { privateVar = parseInt(val); // restrict type }
this.publicVar = "Hello World";
}
MyClass.prototype.extension = function() {
// Here 'this.publicVar' can be set but 'this.privateVar'
// and 'privateVar' are not a valid references.
}
Long story short, the class analog is semantically no different than the JavaScript prototype when done correctly.
- raganwald 13y agoEncapsulating instance state us not the point, encapsulating the class behaviour is the point. So, if you can make the prototype itself private and modify the prototype with class methods, you have something that prototypes by themselves do not express. The analogy is not to a private instance variable in Ruby, the analogy is to a class method in Ruby Dutch as define_method.