Question

I have a question regarding the usage of "use strict" variables.

Why does the following fail silently instead of throwing an error?

"use strict"; 
var $class = {};
$class.rowsICanDisplay = 10;
$class.difference = -1;
var absDifference = 1;
var gridTableBody = $('#mytable tbody');
//code removed for clarity
if($class.difference > 0) {
    var offset = $class.rowsICanDisplay - absDifference; // mistake should be declared in outer scope
    //code removed for clarity
    $('tr:lt(' + offset + ')', gridTableBody).remove();
}
else {
     //code removed for clarity
     $('tr:gt(' + offset + ')', gridTableBody).remove(); // why does this fail silently
}
Was it helpful?

Solution 2

Because Javascript doesn't have block scope.
Any variable declared anywhere in a function is visible to the entire function.

JSHint will catch this problem.

OTHER TIPS

$('tr:gt(' + offset + ')', gridTableBody).remove(); // why does this fail silently

Because offset is not defined in the context of your else block, so your selector is invalid and isn't matching anything.

In the else statement, offset is not defined. You need to pull it out and define it outside of the "if" structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top