This is the 14th day of my participation in Gwen Challenge

Instead of nested conditional branches, use return to prematurely exit the function

 function del( obj ){
 var ret;
 if ( !obj.isReadOnly ){ // Not read-only can be deleted
  if ( obj.isFolder ){ // If it is a folder
   ret = deleteFolder( obj );
  }else if ( obj.isFile ){ // If it is a fileret = deleteFile( obj ); }}return ret;
};

function del( obj ){
 if ( obj.isReadOnly ){ // Invert the if expression
  return;
 }
 if ( obj.isFolder ){
  return deleteFolder( obj );
 }
 if ( obj.isFile ){
  returndeleteFile( obj ); }};Copy the code

2, rational use of the cycle

// What browser is it
function getBrowser(){
 const str = navigator.userAgent;
 if (str.includes('QQBrowser')) {
 return 'qq';
 } else if (str.includes('Chrome')) {
 return 'chrome';
 } else if (str.includes('Safari')) {
  return 'safri';
 } else if (str.includes('Firefox')) {
  return 'firefox';
 } else if(explorer.indexOf('Opera') > =0) {return 'opera';
 } else if (str.includes('msie')) {
  return 'ie';
 } else {
  return 'other'; }};// Loop judgment, the corresponding relationship is abstracted to the configuration, more clear and explicit
function getBrowser(){
 const str = navigator.userAgent;
 const list = [
  {key: 'QQBrowser'.browser: 'qq'},
  {key: 'Chrome'.browser: 'chrome'},
  {key: 'Safari'.browser: 'safari'},
  {key: 'Firefox'.browser: 'firefox'},
  {key: 'Opera'.browser: 'opera'},
  {key: 'msie'.browser: 'ie'},];for (let i = 0; i < list.length; i++) {
  const item = list[i];
  if (str.includes(item.key)) {return item.browser};
 }
 return 'other';
}
Copy the code

3. Refine the function

  • Avoid super-large functions.
  • Separate functions facilitate code reuse.
  • Independent functions are easier to overwrite.
  • A separate function with a good name acts as a comment in itself.
  • Semanticizing the implementation of separate pieces of logic in separate functions allows the code to be logically clear and clearly see what each step is doing.

4, rounded up

/ / a single ~
 console.log(~1337)    / / - 1338

 // Digital input
 console.log(~~47.11)  / / - > 47
 console.log(~~-12.88) / / - > - 12
 console.log(~~1.9999) / / - > 1
 console.log(~~3)      / / - > 3

 // Conversion failed
 console.log(~~[]) / / - > 0
 console.log(~~NaN)  / / - > 0
 console.log(~~null) / / - > 0

 // Conversion fails if the value is greater than a 32-bit integer
 console.log(~~(2147483647 + 1) = = = (2147483647 + 1)) / / - > 0
Copy the code

5. Use pure functions

/ / pure functions
function sum (b, c) {
    return b + c;
}
Copy the code

6. Switch is preferred over if-else when the number of judgment conditions increases

if (color == 'blue') {}else if (color == 'yellow') {}else if (color == 'white') {}else if (color == 'black') {}else if (color == 'green') {}else if (color == 'orange') {}else if (color == 'pink') {}switch (color) {
    case 'blue':

        break
    case 'yellow':

        break
    case 'white':

        break
    case 'black':

        break
    case 'green':

        break
    case 'orange':

        break
    case 'pink':

        break
}
Copy the code

7. Frozen objects

This is the strictest tamper-proof level. Frozen objects are not extensible, sealed, and cannot be modified

let person = { name: '111' };
Object.freeze(person);
 
person.age = 20;
delete person.name;
person.name = '22'
 
person.age // undefined
person.name / / 111
Copy the code