javascript - understanding Complex inArray ternary operator -
i going through inarray method code , came across following ::
inarray: function (elem, arr, i) { var len; if (arr) { if (indexof) { return indexof.call(arr, elem, i); } len = arr.length; = ? < 0 ? math.max(0, len + i) : : 0; (; < len; i++) { // skip accessing in sparse arrays if (i in arr && arr[i] === elem) { return i; } } } return -1; }, now understand how tenary operators work , can tell me , how below line of code works ? ternary operator ?
i = ? < 0 ? math.max( 0, len + ) : : 0; or kind of new construct in js ?
thank you.
alex-z.
original statement:
i = ? < 0 ? math.max(0, len + i) : : 0; to understand better,
i = ? (i < 0 ? math.max(0, len + i) : i) : 0; // ^ ^ yes, nested ternary operator ? :.
following if else representation of above statement, represented in if..else step step.
if (i) { = < 0 ? math.max(0, len + i) : i; } else { = 0; } it works follow:
if (i) { if (i < 0) { = math.max(0, len + i); } else { = i; } } else { = 0; }
Comments
Post a Comment