I am adapting the sql-hint.js code from the excellent CodeMirror to my evil designs...
But I have encountered a line of JavaScript which is beyond me. I don't understand it, and cannot find anything that explains it.
It is line 34 in CodeMirror-master\addon\hint\sql-hint.js and it looks like this:
function getItem(list, item) {
if (!list.slice) return list[item]; // <--- THIS LINE!!!!!
for (var i = list.length - 1; i >= 0; i--) if (getText(list[i]) == item)
return list[i];
}
The command I do not understand is this:
if (!list.slice)
There are no parameters, and there are no brackets. What on earth is going on here? I assume magic...
Please note that it does not say this:
if (!list.slice())
JavaScript is a truthy/falsy language which means that we can assert whether or not a given object is true or false based on some criteria. In JavaScript the values that always report as false are: false
, 0 (zero)
"" (empty string)
, null
, undefined
, NaN
.
The check here is looking to find out if list.slice
is undefined
and if so, is returning the list item at index item
.
If you were to console.log(list.slice)
you'd expect something like function () {}
. We wouldn't want to call the function though since that would fail if slice
was undefined.