Skip to content

Commit

Permalink
added README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
agate committed Mar 23, 2010
1 parent e442fe3 commit abb47a2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Empty file removed README
Empty file.
65 changes: 65 additions & 0 deletions README.md
@@ -0,0 +1,65 @@
* [Features](http://code.google.com/p/js2lang/wiki/Features) (simple documentation)
* [Installation](http://code.google.com/p/js2lang/wiki/Installation)
* [Source Code](http://github.com/jeffsu/js2)

# OO Javascript

For every Javascript developer that has ever wanted to create a library for his/her project, there comes a time when an Object Oriented approach is necessary (or extremely desired). Fortunately, there are a plethora of options to choose from:

> using prototype (not the framework) and hashes:
> var MyClass = function () {
> this.member1 = "member";
> };
> MyClass.prototype = {
> method1: function () { alert('method1 called'); }
> }
> embedding functions right in the instantiator:
> function MyClass () {
> this.member1 = "member1";
> function method1 () {
> alert("method1 called");
> }
> }
> using jQuery (or any of the js OO frameworks) by passing in hashes:
> var MyClass = Class.create({
> member1: "member1",
> method1: function(){ alert("method1 called") },
> });
Unfortunately, these solutions are dissimilar to Java, Ruby, and C-based languages.

# JS2 Solution

JS2 language that is a superset of Javascript and the problem it tries to solve is bringing "natural" OO syntax to Javascript by adding a compilation layer.

So in myClass.js2 one could write:

class MyClass {
var member1 = "member1";

function method1 () {
alert("method1 called");
}
}

And after compilation myClass.js (notice the .js extension vs the .js2) would be:

var MyClass = function () { };
MyClass.prototype = {
member1: "member1",
method1: function () { alert("method1 called"); }
}

One of the nice things about this solution is that it allows us to "calculate" things like mixins and inheritance at compile time rather than runtime. This compilation layer opens the doors for features such as:

* Inheritance
* Mixins (Ruby's multiple inheritance solution)
* getters and setters
* IoC (Dependency Injection)
* AOP (Aspect Oriented Programming)
* foreach
* currying
* More Features

# Inspiration

HAML, SASS, Ruby, Perl, jQuery, Prototype.js

0 comments on commit abb47a2

Please sign in to comment.