简体   繁体   中英

javascript broken syntax

Alright, I or someone I work with broke the syntax here somewhere, and I'm not sure where, as the debugger is giving me some random garble as the error. Anyway here is the function, I think I'm missing a bracket somewhere, but this is just evading me for some reason.

var sort_by = function(field, reverse, primer) {

   var key = function (x) {return primer ? primer(x[field]) : x[field]};

   return function (a,b) {
       var A = key(a), B = key(b);
       return ((A < B) ? -1 : (A > B) ? +1 : 0)) * [-1,1][+!!reverse];                  
   }
}

there's an extra closing parenthesis on the line

return ((A < B) ? -1 : (A > B) ? +1 : 0))

should be

return ((A < B) ? -1 : (A > B) ? +1 : 0) ...etc

It would be useful if could provide the debugger error anyway. I exectued it in Chrome Developer Console and it gave the error:

SyntaxError: Unexpected token )

Which made it easy to find this broken line:

return ((A < B) ? -1 : (A > B) ? +1 : 0)) * [-1,1][+!!reverse]; 

You have unbalanced parenthesis. It should be:

return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse]; 

There's one extra closing bracket here. Remove it.

return ((A < B) ? -1 : (A > B) ? +1 : 0)) * [-1,1][+!!reverse];

Also, semicolon everything.

var sort_by = function(field, reverse, primer) {
   var key = function(x) {
       return primer ? primer(x[field]) : x[field];
   };

   return function(a, b) {
       var A = key(a), B = key(b);
       return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1, 1][+!!reverse];                  
   };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM