Javascript coding format between hash and class

33 Views Asked by At

Hi all i had started by javascript coding in functional style implementation but later when my code got more complex and i started to revamp coding using modular approach. This is my first approach with code 1.

Code 1 :

var ListModel={
    _items:[],
    _selectedIndex:-1,
    getItems : function () {
        return [].concat(this._items);
    },
    addItem : function (item) {
        this._items.push(item);
    }
}

And later in different tutorial i got this kind of approach.

Code 2:

function ListModel(items) {
    this._items = items;
    this._selectedIndex = -1;
}

ListModel.prototype = {
    getItems : function () {
        return [].concat(this._items);
    },

    addItem : function (item) {
        this._items.push(item);
    },

    removeItemAt : function (index) {
        var item;
        item = this._items[index];
        this._items.splice(index, 1);
    }
}

Which one is better and what these code signify as compared to functional style approach. ?

Please help me ...

0

There are 0 best solutions below