Unexpected initialization return value when initialising Datatables inside an Object

StackOverflow https://stackoverflow.com/questions/7305327

  •  25-10-2019
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top