4 ms·
There are so many wrong things about this article, I don't even know where to start. * Every time you call createNew(), all the methods and all the properties
by sktrdie 15y ago
There are so many wrong things about this article, I don't even know where to start.
* Every time you call createNew(), all the methods and all the properties are re-allocated. This is bad for performance and memory consumption. This is why properties and methods should be added to the "prototype" of an object, so that they don't get re-allocated every time you want to create an instance.
* The inheritance example is not actually inheritance but composition. They are two separate things: http://www.artima.com/designtechniques/compoinh.html http://www.artima.com/designtechniques/compoinh.html
* Stuffing everything inside a createNew() function is not minimalist, it's just bad code.
* It refers to "this", "new" and "prototype" as "confusing JS stuff". This obviously means he/she doesn't know anything about the JavaScript programming language.
* JavaScript provides a basic keyword called "new" that easily lets you create a new object. Changing the API naming convention to something called "createNew", is bad practice and will confuse developers trying to use your code. Stick with "new".
If you write code this way, not only it will quickly become slow, but it will also become hard to have other developers contribute to your code base because of the weird conventions.
If you want to write real minimalist JS code, try reading Eloquent JavaScript instead: http://eloquentjavascript.net/ http://eloquentjavascript.net/
- TazeTSchnitzel 15y agoBy not using "new" they aren't changing the API naming convention. They're just not using the traditional OOP model. If you don't use new, you can pass down secrets and properties and compose an object in multiple layers.
- sktrdie 15y agoWhat do you mean by "If you don't use new, you can compose an object in multiple layers"? If you want to compose objects the correct way using prototypes, then you need to use new at some level. If you don't use it, then you're re-allocating the entire object's properties and methods every time you need to use it. You can do it as Crockford does with his "beget object" abstraction: http://javascript.crockford.com/prototypal.html http://javascript.crockford.com/prototypal.html But you still need to use "new". And personally, I'm not that convinced about promoting these sorts of abstractions as Crockford does. It's just really small syntax sugar around "new" and it's totally subjective. So most of the time I simply feel more comfortable using "new".
- MatthewPhillips 15y agoYou can use Object.create and suffer not at all.
- gabordemooij 15y ago* Yes createNew is not very optimal, in practice this is not really an issue though. Furthermore I value maintainability over performance. * They share the same object, so it's not composition. Does it really matter that it is not 'pure' inheritance? * createNew() - bad code? why? just an opinion. * I have written JS apps from day 1. And yes I know how to handle this,new and prototype. Still, I choose no longer to. * obj.new() can be confused with new obj(); That's why I have chosen a more distinctive method name; createNew.
- stephenr 15y ago> I have written JS apps from day 1. And yes I know how to handle this,new and prototype. Still, I choose no longer to And yet you claim: > I dont use new, prototype and this because I find them confusing > obj.new() can be confused with new obj(); That's why I have chosen a more distinctive method name; createNew Also, "dog" can be confused with "do", and "ford" confused with "for" - i can't count how many time's i've got syntax errors because my dog loop of nested ford loops just kept outputting "dogs can't drive, stupid" to the console.
- justncase80 15y ago"new" is more minimal than "createNew" so I think it would be technically more minimal to use "new". Just sayin'.
- jstsch 15y agoEvery time you call createNew(), all the methods and all the properties are re-allocated. This is bad for performance and memory consumption. This is why properties and methods should be added to the "prototype" of an object, so that they don't get re-allocated every time you want to create an instance. Whilst this is true, isn't this something for the Javscript engine to optimize? And doesn't this (very common construct) already get optimized by modern engines? Guess I got a nice Sunday-evening experiment to set up... Language-wise, I find the method the author describes to be very elegant. Everyone seems to write OOP-Javascript in their own way. And the new keywords in ECMAScript 5 aren't helping to resolve these inconsitencies...
- jstsch 15y agoI just gave this a go. In both Safari and Firefox I cannot see a performance difference. Good. I say give this method a go :) But maybe my experiment is flawed: https://gist.github.com/2340375 https://gist.github.com/2340375