How to structure the parameters in a javascript object

0

I have the following code:

 var colHeaders = [
    "Column 1",
    "Column 2"
];

var columns = [
    {data: 'column_1', validator: validatorRentalCode, allowEmpty: false, allowInvalid: false},
    {data: 'column_2', type: 'numeric', numericFormat: typeNumber, allowEmpty: false},
];

var options = {
    columns: columns,
    colHeaders: colHeaders
};
div = document.querySelector('#table');
var table = new Table(div, options);

How variables are part of the context of the Table Object. I would like to improve and transform the variables into parameters as below:

var Table = {
    TABLE: {
        HEADERS: [
            "Column 1",
            "Column 2"
        ],
        COLUMNS: [
            {data: 'horas_extras', type: 'time', timeFormat: 'hh:mm', correctFormat: true, allowEmpty: false}
        ]
    },
    OPTIONS: {
        columns: this.TABLE.COLUMNS,
        colHeaders: this.TABLE.HEADERS
    }
};

In this my change ended up with a undefined : Cannot read property 'COLUMNS' of undefined . Because I have a basic knowledge of the language, I believe that the COLUMNS property does not exist at the moment of object creation.

What would be the best way to solve this problem?

    
asked by anonymous 12.09.2018 / 16:16

1 answer

1

Actually what is giving undefined would be its this.TABLE because when executed this within its object it is catching the scope from outside, not the object.

A valid deal would be:

var gerarTable = function () {

    var columns = [{data: 'horas_extras', type: 'time', timeFormat: 'hh:mm', correctFormat: true, allowEmpty: false}];
    var headers = ["Column 1","Column 2"];

    return {
        TABLE: {
            HEADERS: headers,
            COLUMNS: columns
        },
        OPTIONS: {
            columns: columns,
            colHeaders: headers
        }
    };
};

and running it returns you to Table:

var Table = gerarTable();

One of my tips is to open the browser console (F12) and start playing on the console with the javascript codes, it has access to the javascript page.

    
12.09.2018 / 19:53