views:

119

answers:

3

I am working on a simple problem to represent a few types that have a hierarchical structure. There is a data row that contains some data, and the data could vary from type type of row to another. A simple row might only have a title and date, while an extended row may contain a title, description, date and an image. I am not new to Javascript but don't understand it well enough to proceed forward. To give a simplified example, here is how I would write it in Java:

interface Row {
    View getView();
}

class BasicRow implements Row {
    private String title;
    private String description;

    public BasicRow(String title, String description) {
        this.title = title;
        this.description = description;
    }

    public View getView() {
        // return a View object with title and description
    }
}

class ExtendedRow implements Row {
    private String title;
    private String description;
    private Date date;
    private Image image;

    public ExtendedRow(String title, String description, Date date, Image image) {
        this.title = title;
        this.description = description;
        this.date = date;
        this.image = image;
    }

    public View getView() {
        // return a View object with title
        // description, date, and image
    }
}

There are few OO improvements that can be done here such as extending ExtendedRow from BasicRow and only defining the new fields plus overriding the getView method.

Javascript does not have interfaces or abstract classes and I am afraid I haven't gone down to thinking in a prototypical sense yet. So how could I go about implementing something as basic as the above example in Javascript where there is a base class or an interface, and two classes extending from that base class, each with their own specific behavior.

Any pointers are greatly appreciated.

+2  A: 

Have a look at John Resig's article on inheritance in Javascript. Resig's work and also Mootools.class (part of the Mootools library) are both derivative work of Dean Edward's Base.js

Hope that helps.

Gregory Pakosz
thanks for the links. I really like the MooTools `Class` and API design in general, and reading this rather long article now http://jqueryvsmootools.com to decide whether to use MooTools over jQuery for my project.
Anurag
I think it's a mistake trying to force JavaScript into a classical OO mould. I prefer Crockford's embracing of JS's features.
Skilldrick
indeed, but I just pointed that classical OO is feasible
Gregory Pakosz
+5  A: 

Take a look at Douglas Crockford's articles:

Douglas Crockford is a senior JavaScript architect at Yahoo! He is well known for his work in introducing JavaScript Object Notation (JSON) - Wikipedia

These articles are must-reads, and I'm sure they will help you figure out how to structure your objects.

roosteronacid
those were all indeed great reads.. it does take a major shift in thinking and i guess i will be hacking away at the minor details all day, but these links really helped clear up a lot of doubts
Anurag
Glad it worked out for you. I had about the same experience when reading those three articles the first time :)
roosteronacid
+1  A: 

"Javascript does not have interfaces or abstract classes and I am afraid I haven't gone down to thinking in a prototypical sense yet."

You don't need interfaces in typeless languages. Interfaces just allow the compiler to make sure an object has certain functionality so it can typecheck. With Javascript, you just call the method you want and hope (well, make sure on your part... js won't help you) that the object you are calling it on has that functionality.

trinithis