我如何在 javascript 中访问函数外部的变量

我想在函数外访问 const newdata


    function updateFields(response) {

        $("#FIELDSET").removeProp('disabled');


        const parser = new DOMParser();

        const xmlDoc = parser.parseFromString(response, "text/xml");

        const data = xmlDoc.getElementsByTagName("responses")[0].innerHTML;

        const rdata = data.split('//')[0]

        const payload = data.split(':1:1,')

            .map(x => x.split('='))

            .reduce((obj, x) => {

                obj[x[0].replace(/\./g, '_')] = x[1];

                return obj;

            }, {})


        const newdat="string";



        CO_CODE=payload.CO_CODE.split(':1:1')[0]

        const {AMOUNT, INT_RATE, TERM, FREQUENCY, REP_START_DATE, CUSTOMER_ID, INSTALMENT} = payload;


    }


红糖糍粑
浏览 258回答 3
3回答

慕勒3428872

如果newdat不是特定于——或特别依赖于——你的updateFields方法,你可以在updateFields范围之外维护它;每当updateFields被调用时更新它。let newdat = '';function updateFields() {    // ...    newdat = "string";    // ...}function someOtherFunction() {    console.log( 'newdat =' + newdat ); // would log the current value of `newdat` when called}let newdat = '';function updateFields() {  newdat = "string";}function someOtherFunction() {  console.log('newdat = ' + newdat); // would log the current value of `newdat` when called}updateFields();someOtherFunction();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript