Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Cobra: A Little JavaScript Class Library (harmonize.fm)
18 points by jmtulloss on Jan 12, 2009 | hide | past | favorite | 12 comments


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?


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.


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.


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.


if you want something like a singleton why do you just:

  var singleton = function (args) {
  
  /*object constructor stuff*/
  
  }(arg-values)


That's another way to do it. The problem is that this way and other ways are not similar, making it hard to go from one form to the other. It's also not the same as declaring a class.

I talk a bit more about what I'm trying to fix earlier: http://justin.harmonize.fm/index.php/2008/12/a-better-object...


Did you mean:

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

Instead of:

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

?


Yes. Thanks!


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.


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.


Have a look the library I posted to github just yesterday. It maintains javascript's prototypal inheritance but gets rid of the BS:

http://github.com/richcollins/crux/tree/master/Crux.js


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

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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: