1

config.jsファイルのrequire()アクションを介して(オブジェクト型の)モジュールを含むjavascriptファイル(newconfig.jsと呼びましょう)があります:

core.js を次のように考えます。

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'
        }
        ]
     }     
 };

私の newconfig.js ファイルには以下が含まれています。

const core = require('./core/config');

次に、ファイル内のコアのインスタンスを複製します。

let config = Object.assign({}, core);

次に、ローカル オブジェクトを変更します

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>',
    }
];

これを、コア構成を拡張する別の構成としてエクスポートできるようにします。

module.exports = config;

外部ファイルが ./core/config ファイルをローカルに含めて使用しようとすると、newconfig.js が変更されます。

IE (mylayout.js):

const core = require('./core/config');
console.log(core);

出力時のコア値は次のとおりです。

    {
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>',
            }
        ]
      }
}

オブジェクトを変更する前に新しいオブジェクトに複製し、その新しいオブジェクトをエクスポートすると、元のコア構成が変更される原因はどこにありますか?

別の js ファイルで newconfig.js を要求すると、望ましい動作が返されます。

{
    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>',
            }
        ]
      }
}
4

2 に答える 2