Hacker News new | past | comments | ask | show | jobs | submit login

Instead of using prototypes and functions shared among all instances of the same type of object, it uses closures for each instance. The coding style is also questionable, with everything referencing a variable named 'I'.

It works, but in my opinion it is absolutely not a correct way to write code. In any case, OOP and class-style programming in Javascript is a large topic.




As you suggest, there are several schools of thought about how best to provide class-style programming. By using a closure instead of prototypes, you can have "true" private properties and functions. If you use prototype, you'll need to use a convention to mark something as private and hope the developers respect that (marking it with an underscore is common). In my experience, someone will ignore the convention and then something will break when you change or remove your "private" variable.


If the code was using those or other advantages of the pattern (like easier subclassing, methods already bound to the instance, etc) consistently, it would make sense. As it is, it just does ugly things like using 'I' and 'this' without any thought or pattern I can discern.


Style is one thing, but if you use closures, you have to be a lot more careful about memory leaks.


Thanks.

> Instead of using prototypes and functions shared among all instances of the same type of object, it uses closures for each instance.

So, is this generally considered a poor practice? I usually only use prototypes and shared functions when I am going to be instantiating the same type of object many times (when doing lots of vector math, for example).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: