为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红糖糍粑 2021-06-30 14:01:43
我想在函数外访问 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;    }
查看完整描述

3 回答

?
慕勒3428872

TA贡献1848条经验 获得超6个赞

如果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();


查看完整回答
反对 回复 2021-07-01
  • 3 回答
  • 0 关注
  • 257 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信