简体   繁体   中英

Why default properties in external JS file were not overwritten with user defined properties in object?

I have an HTML and JS file. Should the user want to overwrite the default settings that are declared in the external JS file all they have to do is include the following as an inline script:

<script>
    userControls = { 
        transition : 'fade',
        nextText : 'Next'
    }
</script>

The problem I am facing is that my external script is not picking up the user settings and is setting everything as default.

 var defControls = { transition : 'default', nextText : 'Next &raquo;' }; var userControls = {}; // CHECKS FOR userControls if (Object.getOwnPropertyNames(userControls).length > 0) { var controls = Object.assign({}, defControls, userControls); } else { controls = defControls; } console.log(userControls); console.log(controls); console.log(defControls); 
 <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="styles.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- EXTERNAL SCRIPT THAT ACCEPTS USER'S NEW SETTINGS --> <script type="text/javascript" src="script.js"></script> </head> <body> <!-- SENDS THE NEW PARAMETERS TO script.js --> <script> userControls = { transition : 'fade', nextText : 'Next' } </script> </body> </html> 

It's because that you inserted the user choice after your js file loads. It goes through the following control first

if (Object.getOwnPropertyNames(userControls).length > 0)

so this will always evaluate to false . That's why you keep getting default values. You should either insert the user choice script first or some async functionality in the js file.

You have four and half mistakes in your code:

  1. You have to write inline script with code: var userControls = {transition: 'fade', nextText: 'Next'}; before external JS file. And do not forget ; on the end (it is the half mistake).
  2. The line var userControls = {}; in external JS file has overwrited the settings in userControls in inline script . I think you wanted to write instead var controls = {}; .
  3. The var controls = {}; you have to write before if...else statement.
  4. You write console.log(userControls); instead of console.log(JSON.stringify(userControls)); Your code writes only [object Object] in developer console from IE. But in Chrome it is okey.

Solution with corrected code

 <!DOCTYPE html> <html><head> <!-- <link type="text/css" rel="stylesheet" href="styles.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> --> <!-- SENDS THE NEW PARAMETERS TO script.js --> <script type="text/javascript"> var userControls = { transition : 'fade', nextText : 'Next' }; </script> <!-- EXTERNAL SCRIPT THAT ACCEPTS USER'S NEW SETTINGS src="script.js" --> <script type="text/javascript"> var defControls = { transition : 'default', nextText : 'Next &raquo;' }; //THIS WAS THE MISTAKE: var userControls = {}; this line has overwrited the settings var controls = {}; // CHECKS FOR userControls if(Object.getOwnPropertyNames(userControls).length > 0) { controls = Object.assign({}, defControls, userControls); } else { controls = defControls; } console.log('userControls =\\n' + JSON.stringify(userControls, null, '\\t')); console.log('controls =\\n' + JSON.stringify(controls, null, '\\t')); console.log('defControls =\\n' + JSON.stringify(defControls, null, '\\t')); </script> </head> <body></body> </html> 

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