News for hackers. Stuff that matters.
Re:JS.Class - A Ruby Inspired JS Framework by papertiger on Saturday March 27, 2010 @05:25PM (#1223711)
Looks nice, but I'm not sure why JavaScript needs to be like Ruby.
Re:JS.Class - A Ruby Inspired JS Framework by richcollins on Saturday March 27, 2010 @05:36PM (#1223722)
Lack of classes is one reason to prefer JavaScript to Ruby. People always feel the need to add complexity where it isn't needed.
Re:JS.Class - A Ruby Inspired JS Framework by jrockway on Saturday March 27, 2010 @08:55PM (#1224007)
Yes, the lack of the class abstraction does add complexity to your code. That's why frameworks like this exist.
Re:JS.Class - A Ruby Inspired JS Framework by richcollins on Sunday March 28, 2010 @01:04PM (#1224852)
I was making the opposite assertion. How are classes simpler than prototypes? If you want to add behavior to the proto and the clone, you just add a method to the proto. If you want to add behavior to a class and its instances, you have to do a fair bit more.
Re:JS.Class - A Ruby Inspired JS Framework by jrockway on Monday March 29, 2010 @02:57PM (#1227251)
Prototypes offer no contract, so it's up to the consumer to ensure that the object received is acceptable. Classes offer a contract, so if an object is an instance, it's guaranteed to work (Liskov, etc.).

Basically, the idea of OOP is information hiding and polymorphism, and prototype-based objects throw both of these concepts away. The result is more complicated code in general.

Re:JS.Class - A Ruby Inspired JS Framework by richcollins on Monday March 29, 2010 @11:11PM (#1228244)
Ruby Classes don't provide a contract either. You can add and remove methods to your heart's desire.

information hiding is the principle of segregation of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed

http://en.wikipedia.org/wiki/Information_hiding

How are prototypes inferior to classes with respect to information hiding? They provide the ability to easily change the implementation while maintaining the interface.

How do prototypes inhibit polymorphism? All of the prototype languages that I know of (Javascript, Io, Self, Lua ...) make it easy to take advantage of polymorphism.

Re:JS.Class - A Ruby Inspired JS Framework by jpcx01 on Saturday March 27, 2010 @07:43PM (#1223918)
There's a lot of useful functionality here that comes in handy. Just as a standard library, I think its useful.

Ruby Hashes and Enumerators have way more functionality than the stock js types.

Is it worth the 40k minimized for the full include? That I dunno.

Re:JS.Class - A Ruby Inspired JS Framework by richcollins on Sunday March 28, 2010 @01:05PM (#1224855)
What do those have to do with Ruby? Those things have nothing to do with Classes, Modules private / public ivars ...
Re:JS.Class - A Ruby Inspired JS Framework by alanstorm on Saturday March 27, 2010 @07:21PM (#1223885)
Less context switching when you're developing web applications that require a significant amount of Javascript code. Less context switching means you get to stay in flow and get more done.
Re:JS.Class - A Ruby Inspired JS Framework by jagtesh on Sunday March 28, 2010 @02:08AM (#1224278)
It's an interesting experiment as far as I can see. Something on similar lines as http://phpjs.org/
Re:JS.Class - A Ruby Inspired JS Framework by sshumaker on Saturday March 27, 2010 @05:20PM (#1223704)
I implemented something like this for my last project, but I think you can make JS class creation far more ruby-like (and flexible, for that matter), by not passing an explicit hash to your class creation function, but instead building the class in a callback:

  // code start
  var Event = Class.define(function()
  {
    this.speak = function()
    {
       console.log("HI");
    }

    // since you can just write code in here
    // metaprogramming is easy:
    ["attack", "defend"].each(function(name) {
    this[name] = function() {console.log(name);}
    }.bind(this));

    // or build reusable metaprogramming facilities
    // and use them here
    this.$setters(["x", "y", "z"]);
    this.$memoize("expensiveFunc", function() {
         // blah
    });
    this.$inherited = function(derivedClass) { ... }
});

Basically, the class define method would create a special 'class-creation' object, and would invoke the user provided function with the class creation object as the this pointer. After the callback finished, I would use the class-creation object to build the actual class.

Being actual able to write code inside your class definition is a much more flexible way to define your objects - and it's also how Ruby does it.

Re:JS.Class - A Ruby Inspired JS Framework by cloudhead on Saturday March 27, 2010 @08:29PM (#1223984)
Thanks, but no thanks. Prototypal inheritance is JavaScript's strength.
Re:JS.Class - A Ruby Inspired JS Framework by sreque on Saturday March 27, 2010 @10:47PM (#1224120)
You can do prototype-style inheritance in ruby, but no one does it. In fact, most new languages that come out express no interest in this style. It makes me wonder why the Javascript people are so convinced that why they have is that good.
Re:JS.Class - A Ruby Inspired JS Framework by invisible on Sunday March 28, 2010 @12:35AM (#1224218)
I don't know - I love PHP classes but I'd love to see PHP with prototype-style inheritance (if PHP had classes for strings, etc. in respect to basic variable types). I think the "everything is an object" route is great and coupling that with prototypes exemplifies that decision's greatness.

However, I really do think that JavaScript needs all of the great things ECMA 5 displays first and foremost (such as strict mode, getters/setters/enumerable/configurable/writable for property descriptors).

John Resig does an excellent job of summing up ECMA 5 pragmatically (it truly must be great to work on JavaScript stuff as a day job): http://ejohn.org/blog/ecmascript-5-objects-and-properties/

Re:JS.Class - A Ruby Inspired JS Framework by raganwald on Saturday March 27, 2010 @05:55PM (#1223753)
Just like in Ruby, classes and modules are open for modification at any time, so you can add and change methods in existing classes.

Brings to mind the expression "A bug-for-bug port of the existing system."

Re:JS.Class - A Ruby Inspired JS Framework by jrockway on Saturday March 27, 2010 @08:56PM (#1224010)
I think Joose is a more featureful variant of this idea:

http://code.google.com/p/joose-js/

Re:JS.Class - A Ruby Inspired JS Framework by JimBastard on Sunday March 28, 2010 @04:30AM (#1224355)
Coffeescript
Re:JS.Class - A Ruby Inspired JS Framework by jagtesh on Sunday March 28, 2010 @01:06PM (#1224860)
Rocks. I love JS for what it is, even though I would equally love for CoffeeScript to be mainstream and natively supported across all browsers :)