I have a javascript file (lets call it newconfig.js) that is including a module (of type Object) through a require() action in a config.js file:
Consider core.js to be:
module.exports = {
configuration: {
showLanguageSelector: false
},
tableStructure: {
columns: [
{
tooltip: 'Indicates if this load has alerts or notes',
name: 'Alerts <em>& Notes</em>'
},
{
tooltip: 'Trailer number and trailer type',
name: 'Trailer <em>Type</em>'
},
{
tooltip: 'Door number',
name: 'Door'
},
{
tooltip: 'Trailer opened date/time',
name: 'Open<span>ed</span>'
},
{
tooltip: 'Trailer closed date/time',
name: 'Closed'
}
]
}
};
My newconfig.js file contains:
const core = require('./core/config');
I then clone the instance of core in my file:
let config = Object.assign({}, core);
I then mutate my local object
config.Configuration = {
showLanguageSelector: true
};
config.tableStructure.columns = [
{
tooltip: 'Indicates if this load has alerts or notes',
name: 'Alerts <em>& Notes</em>',
}, {
tooltip: 'Trailer number and trailer type',
name: 'Trailer <em>Type</em>',
}
];
so that I can export this as another config that extends the core configuration:
module.exports = config;
When an external file attempts to include the ./core/config file locally to use, it has the changes of newconfig.js
IE (mylayout.js):
const core = require('./core/config');
console.log(core);
the core value when outputted is:
{
Configuration: {
showLanguageSelector: false // interesting how this wasn't mutated!!!!!
},
tableStructure {
columns: [
{
tooltip: 'Indicates if this load has alerts or notes',
name: 'Alerts <em>& Notes</em>',
}, {
tooltip: 'Trailer number and trailer type',
name: 'Trailer <em>Type</em>',
}
]
}
}
Where am I going wrong that is causing my original core config to get mutated, if I am cloning the object to a new object before changing it, and exporting that new object?
requiring my newconfig.js in another js file returns the desired behavior:
{
Configuration: {
showLanguageSelector: true
},
tableStructure {
columns: [
{
tooltip: 'Indicates if this load has alerts or notes',
name: 'Alerts <em>& Notes</em>',
}, {
tooltip: 'Trailer number and trailer type',
name: 'Trailer <em>Type</em>',
}
]
}
}