4 ms·
Recently I have been having a good time developing a large program with this classical inheritance pattern. Person = function() { this.say = function(phr
by bni 16y ago
Recently I have been having a good time developing a large program with this classical inheritance pattern.
Person = function() {
this.say = function(phrase) {
console.log(phrase);
}; Person.prototype.say = this.say;
};
Developer = function() {
var mypriv = "I have encapsulation";
this.say = function(phrase) {
Person.prototype.say.call(this, "Hello");
console.log("I develop "+phrase);
console.log(mypriv);
};
}; Developer.prototype = new Person();
var devel = new Developer();
devel.say("a lot");
Coming from a C++ and Java background, I have put this pattern together from various sources: Crockford, John Resig and others. It has worked great and I dont see any problems with structuring your large JS program in this way.
- gcb 16y agoi simply avoid writing "new" in javascript. ever. well, unless its for Date. if i really need OO, i use YUI and forget about most javascript caveats. Everything is just too 'wrong' to keep track in your mind.
- bni 16y agoEverything felt wrong when I started programming in JS for real about a year ago, but now I would say I understand most aspects of it, and can keep track. Im still not ready to let go of classes and classical inheritance though. Not sure if I ever want to.
- ichverstehe 16y agoTake a look at Io. It does prototypes properly. And they are, in fact, pretty nice to work with. I wouldn't mind getting rid of classes in my day-to-day life.
- jashkenas 16y agoPlease, please don't spread this "object factory" pattern any further than it's already gone. It's an incredibly inefficient way to do OOP in JavaScript. Every time you create a new instance of a Developer, you redefine every single function. This makes instantiation far slower than it would be if you used prototypes, and leaks memory like crazy: N Developers means N copies of each function. So, first, it's slow -- take this test: https://gist.github.com/459112 https://gist.github.com/459112 Webkit: http://tinyurl.com/2csgzez http://tinyurl.com/2csgzez Firefox: http://tinyurl.com/29wd2ms http://tinyurl.com/29wd2ms And second, the memory use. Take a moderately-sized class, with ten member functions. The last time I benchmarked it in Chrome, using real prototypes, it takes a little under 32 megabytes of RAM to create a million instances of it. Try to create a million instances using the pattern you have above, and it spins the browser for minutes, and eats up 368 megabytes of RAM. IE just crashes. If you're doing OOP in JS -- please use prototypes as Eich intended...
- bni 16y agoThanks for the benchmarks, I know the tradeoff for using this pattern. Im aware of that the function is defined in each object, and that it takes up memory. I dont agree that the memory is leaked, it is reclaimed when the object is deleted. As I never create millions of instances of objects, and this patterns dont seem to have a measurable impact on the rendering loop in my project, Ill just continue using it for now. I would call the pattern you propose to be the "object factory" pattern. And I am curious how to do classical inheritance and encapsulated vars with it.