Question

I put the dataTable function initialization inside an object but I don't get the same result as when I initialize it outside the object

Initialization outside object

var dataTable = $('datatable').dataTable();

Initialization inside object

var aObject = {
    dataTable : null,
    initFunction : function() {
        // this.dataTable contents is not the same when I initialize dataTable outside the object
        this.dataTable = $('datatable').dataTable();
    }
}

Why is this?

EDIT: Also, it doesn't seem to successfully initialise the table to dataTables when done inside an object.

Was it helpful?

Solution

I've tested it using following code, and the results of both console.log are the same. Are you sure that you're using right selector in both places (inside and outside of object) ?

var aObject = {
    dataTable : null,
    initFunction : function() {
        this.dataTable = $('#data').dataTable();
        }
    }

    $(function() {
        var x = $("#data").dataTable();
        aObject.initFunction();

        console.log(x);
        console.log(aObject.dataTable);
    })

and following html table:

<table id="data">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                one
            </td>
            <td>
                22
            </td>
        </tr>
    </tbody>
</table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top