这就是我想要的:
let val;
function setVal(aVal) {
val = aVal;
realValName = magic_happens_here(aVal);
console.log(`${realValName} = ${aVal}`);
}
因此,如果我这样做:
setVal(window.outerHeight); //suppose value is 500
控制台将显示:
window.outerHeight = 500
以下解决方案不起作用:
function setVal(aVal){
val = aVal;
let aName = Object.keys({aVal})[0];
console.log(aName + ' = ' + val);
}
因为它打印以下内容
aVal = 500