dimanche 1 février 2015

Literal strings vs. object properties in JavaScript

JavaScript libraries in the same space (UI widgets) use two different approaches for instantiating components:




  1. Literal strings to specify types, and inlining as much as possible, for example:



    var oTable = webix.ui({
    view: "datatable",
    columns: [
    { id: "rank", header: "", css:"rank", width: 50 },
    { id: "title", header: "Film title", width: 200 },
    { id: "year", header: "Released", width: 80 },
    { id: "votes", header: "Votes", width: 100 }
    ],
    autoheight: true,
    autowidth: true,
    data: tableData
    });



  2. A complex class hierarchy and passing objects instead of strings:



    var oTable = new sap.ui.table.Table({
    title: "Movies",
    });


    oTable.addColumn(new sap.ui.table.Column({
    label: new sap.ui.commons.Label({text: "Film title"}),
    template: new sap.ui.commons.TextField().bindProperty("value", "title"),
    sortProperty: "title",
    filterProperty: "title",
    width: "200px"
    }));

    oTable.addColumn(new sap.ui.table.Column({
    label: new sap.ui.commons.Label({text: "Released"}),
    template: new sap.ui.commons.TextField().bindProperty("value", "released"),
    sortProperty: "released",
    filterProperty: "released",
    width: "80px"
    }));

    oTable.addColumn(new sap.ui.table.Column({
    label: new sap.ui.commons.Label({text: "Votes"}),
    template: new sap.ui.commons.TextField().bindProperty("value", "votes"),
    sortProperty: "votes",
    filterProperty: "votes",
    width: "100px"
    }));

    // add the other columns...

    var oModel = new sap.ui.model.json.JSONModel();
    oModel.setData({modelData: tableData});
    oTable.setModel(oModel);
    oTable.bindRows("/modelData");



In both cases, tableData is a plain JSON array, so no differences there:



[
{ id: 1, title: "Shawshank Redemption", year: 1994, votes: 678790, rank: 1 },
{ id: 2, title: "The Godfather", year: 1972, votes: 511495, rank: 2 }
]


What are the advantages and disadvantages of these two approaches, in terms of learning curve, maintainability, avoidance of antipatterns, reusability, and other software engineering best practices?


Aucun commentaire:

Enregistrer un commentaire