

Cobra: A Little JavaScript Class Library - jmtulloss
http://justin.harmonize.fm/index.php/2009/01/cobra-a-little-javascript-class-library/

======
Jebdm
It seems like a neat idea; it's not the normal Javascript way of operating,
but sometimes classes do make more sense. I don't really get why you did
Singletons, though. Why not just use a normal object?

~~~
jmtulloss
I've run into a number of situations where it's necessary to initialize and
maintain some state in the instance. This is easier done when you have a
constructor.

Admittedly, others might not run into these situations, but it's happened to
me frequently enough to include it.

~~~
Jebdm
I guess it doesn't hurt to have it, especially if you're writing a lot of
singletons, but it just seems strange to me to do it explicitly since
singletons are extremely easy to implement in prototype-based languages.

    
    
      singleton = Object()
      singleton.x = 0
      singleton.foo = function(){
          return x + 1
      }
    

Edit: Also, why not <http://www.prototypejs.org/> ? Not that there's anything
wrong with playing around with stuff of course.

~~~
jmtulloss
I agree, that's what I do now. Adding them is purely for consistency in syntax
(sugar, if you will). Using this library, there is a syntactic consistency
between declaring types and singletons that you don't have normally.

I use prototype on a day to day basis and I'm not a huge fan. Mootools has
captured my attention as being a better alternative, and some of the code was
taken from mootools. This is just classes though, not a full JS library.

------
snprbob86
Did you mean:

self.cats = [new Tiger(), new Tiger(), new Cat()];

Instead of:

this.cats = [new Tiger(), new Tiger(), new Cat()];

?

~~~
jmtulloss
Yes. Thanks!

------
sh1mmer
So, essentially, the author who I assume is a Java programmer doesn't get
Prototypal inheritance and wishes to make Javascript into Java.

It's vaguely interesting looking at how he did it, the actual output itself,
not so much. You'd be better off reading Doug's Javascript the Good Parts than
trying to remake your Javascript into something it's not.

~~~
jmtulloss
I don't even know Java beyond some of the concepts.

If you look at the code, it uses prototypical inheritance. The whole thing is
very lightweight, under 200 lines of code.

I do love "JavaScript the Good Parts", but I do have some issues with the
syntactic inconsistencies that exist in JS, good parts or not.

------
axod
>> a shortcut way of doing ClassName.prototype.functionName = function(args).

A single line assignment already seems quite shortcutish to me...

