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
}
有帮助吗?

解决方案 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.

其他提示

$('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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top