Class.js

Posed under JavaScript on May 8, 2010.

Article

Comments

Post

Rodrigo

Aug 02 2010

Nice works,the begin is now...

HB

Aug 03 2010

Very nice for such short code. This would be great for small (but potentially powerful) web apps or mockups.

Kambfhase

Aug 03 2010

This code is crap. I have published a walktrough over here:

http://gist.github.com/505213

Evan Byrne

Aug 03 2010

Kambfhase, while I respect your opinion and thank you for pointing out a couple things, I have to completely disagree with you.

Most of your comments have to do with your own opinion and not how the actual script performs. I had reasons for doing things the way I did.

I have since fixed the issues with the key variable being global in ES3. Once again, thank you pointing that out.

Kimmo Tapla

Aug 03 2010

The script had (has?) some issues codewise, but I'm more worried about the idea. First, you can't just go around saying that JS does not have classes. It does. Second, you can't write a new class interface in JS to replace the existing stuff without taking a serious performance hit. What you should do instead, is learn more about JS and how you should work with it.

JS has a lot of stupid stuff going on but it also has some very, very nifty things that more than make up for its shortcomings. Prototypal inheritance and soft objects will seem a bit weird at first but they have their strengths.

So, thanks for the effort but I think we should lay this project to rest immediately. Cheers.

Evan Byrne

Aug 03 2010

Kimmo, JavaScript really doesn't have classes like in other languages. It does however allow you to create class-like structures using functions and prototypes like you refer to.

This was created with large, complex applications where maintaining good application structure is problematic. I understand stuff like prototypes and soft objects, so this wasn't created out of ignorance.

If you can get me any benchmarks on how this would slow down applications any measurable amount that would be great.

Also, I worked on an updated version today and will be posting it within the next couple days. The implementation and code quality is much better in the new version.

Kimmo Tapla

Aug 04 2010

So, these native "class-like structures that are not classes" are somehow worse than the class-like structures Class.js produces?

As for performance issues, I can't give you any benchmarks but I can pretty safely say that using Class.js to instantiate objects uses a lot more memory than using prototypes. You see, instances of these "class-like structures" are stored differently than those that are manually copied from an object. The main idea is that a lot of stuff is stored in the prototype and only object-specific stuff is stored in the object itself. If you create an object by looping through all the object properties and copying them individually to the new instance, you'll end up creating an object that relies to the miniscule Object prototype and has all "class-specific" properties (e.g. all methods) stored in the object itself.

And one of the coolest things about prototypes is that you can change classes (or "class-like structures") by modifying the prototypes. Objects instantiated using Class.js don't use prototypes (aside from the Object prototype) and therefore can't benefit from this great feature.

I guess I'll have to stick to my opinion.

Post Comment

Capcha