4 ms·
Classy: Classes for Javascript
- whalesalad 17y agoAnother handy class library that I have been using a while - http://justin.harmonize.fm/index.php/2009/01/cobra-a-little-javascript-class-library/ http://justin.harmonize.fm/index.php/2009/01/cobra-a-little-... Now I tend to write my "classes" like this: http://gist.github.com/380000 http://gist.github.com/380000 This is using the traditional prototype method. It involves a bit more syntax, but it doesn't require any extra libraries.
- jeresig 17y agoHuh, weird. This seems to crib very heavily from my original Simple JavaScript Inheritance implementation: http://ejohn.org/blog/simple-javascript-inheritance/ http://ejohn.org/blog/simple-javascript-inheritance/ It even uses the 'classy' name and logo (stemming from an April Fools joke that I ran that used the aforementioned inheritance implementation): http://ejohn.org/blog/classy-query/ http://ejohn.org/blog/classy-query/ Seems like he may have made some improvements to the code (using hasOwnProperty and putting a try/finally around the code block are two changes that I see) but on the whole it looks very very similar to the original implementation that I wrote. Odd. Update: It seems as if the author credits the original code in a tweet that he made: http://twitter.com/mitsuhiko/status/12902227050 http://twitter.com/mitsuhiko/status/12902227050
- hammerdr 17y agoThe reversal of the "extends" paradigm is interesting. It seems very natural. What made you go with that? (I know I'm not really responding to your comment but I think it speaks for itself).
- jeresig 17y agoGood question - I was inspired heavily by Dean Edwards' original Base Class library, it seemed to make the most sense when I was using it. The original code: http://dean.edwards.name/weblog/2006/03/base/ http://dean.edwards.name/weblog/2006/03/base/ The current code: http://code.google.com/p/base2/source/browse/trunk/src/base2/Base.js http://code.google.com/p/base2/source/browse/trunk/src/base2...
- the_mitsuhiko 17y ago> The reversal of the "extends" paradigm is interesting. It seems very natural. What made you go with that? I think the earliest version of that was prototype which I think used Class.create and Object.extend. I've seen that in many JavaScript class implementations so far.
- the_mitsuhiko 17y ago> Huh, weird. This seems to crib very heavily from my original Simple JavaScript Inheritance implementation: http://ejohn.org/blog/simple-javascript-inheritance/ http://ejohn.org/blog/simple-javascript-inheritance/ It is, see the AUTHORS file which credits that URL. In fact I was using a slightly modified version of your code for a long time but had to do some changes in order to use it for a project of mine. Basically I need classes implemented in a way that they will work in the presence of any other JavaScript framework. Reason: this JavaScript is injected into unknown code by a middleware system. > It even uses the 'classy' name and logo (stemming from an April Fools joke that I ran that used the aforementioned inheritance implementation): http://ejohn.org/blog/classy-query/ http://ejohn.org/blog/classy-query/ Interesting. I did not know that one. Name was rather obvious and did not return any direct hits with projects named like this so I went with it. The original drawing of that logo is the search result on Google images for "classy" with filter set to linedrawing. Original author unknown, I just vectorized it. > ... but on the whole it looks very very similar to the original implementation that I wrote. Odd. If you want a more obvious attribution than the AUTHORS file, just let me know. Other changes besides hasOwnProperty, the ability to use the library without modifying window, and $ prefixed names so that it does not clash with custom attributes is a more python/javascript inspired naming scheme for special methods and attributes (__init__ etc.) and support for mixins. Also attributes are properly removed if they were unset previously and not just reassigned with undefined and classes can be instanciated without new like in Python. Can't count how often my code did funny stuff because I forgot new there :) All in all, the idea is to have that fairly common piece of object orientated JavaScript class bootstrapping under an open source license so that anyone can use it. I hope I succeeded with that.
- maccman 17y agoThis is my take on it - few less dollars and underscores: http://github.com/maccman/supermodel-js/blob/master/superclass.js http://github.com/maccman/supermodel-js/blob/master/supercla...
- sh1mmer 17y agoRather than doing this why not just read "JavaScript: The good part" and learn about JavaScript's classes instead.
- code_duck 17y agoI do wonder why so many people want to have class-based, traditional inheritance in JavaScript. Why not just embrace the prototype based style? I use Crockford's object.beget rather than normal JS constructors, and that works quite fine. On the other hand, the mechanism of classical inheritance is well understood, and I do miss conveniences like __init__ and subclassing.
- the_mitsuhiko 17y ago> I use Crockford's object.beget rather than normal JS constructors, and that works quite fine. Crockford's beget patches on Object which I think is a terrible idea. The main reason why people create class libraries on top of JavaScript is the constructor problem (that setting up the "inheritance chain" triggers the constructor even though we only want the prototype).
- Semiapies 17y agoEmbracing and enhancing prototype style - and perhaps making it clearer in some way - would be very interesting.
- code_duck 17y agoI think that the 'beget' style does just that. Instead of requiring a special constructor, it provides a clear way to duplicate an object - just what prototypical inheritance is meant to be.