Javascript Inheritance

I am working on a simple problem to represent several types having a hierarchical structure. There is a data row containing some data, and the data can vary from row type type to another. A simple line can only contain a title and a date, and an extended line can contain a title, description, date and image. I'm not new to Javascript, but don't understand it well enough to move forward. To give a simplified example, here is how I will 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 several OO improvements that can be made here, for example, extending the ExtendedRow from BasicRow and defining only new fields plus overriding the getView method.

Javascript , , . , - , Javascript, , , , .

.

+3
3

:

- JavaScript Yahoo! JavaScript (JSON) - Wikipedia

, , , .

+7

"Javascript , , ".

You do not need interfaces in non-languages. Interfaces allow the compiler to verify that the object has certain functionality, so it may look like typecheck. With Javascript, you simply call the method you want and want (well, make sure that your part ... js does not help you) that the object you are calling it with has this functionality.

+1
source

Source: https://habr.com/ru/post/1727752/


All Articles