我知道与其他语言相比,javascript 中的数组有点特殊,但我并没有真正理解这种行为,或者这里发生了什么。
我想知道为什么会发生这种情况以及为什么我没有得到一个空数组:
function setupWindowProgBar(settings, window, palette, arr){
console.log('start');
console.log(arr);
if(typeof arr == 'undefined'){
var arr = [];
}
console.log(arr);
console.log('stop');
var arrLen = arr.length;
arr[arr.length] = createProgBar('master', 'bg', window, 0, settings.fillColor, settings.strokeColor, settings.textColor, palette, 'percent', settings.reqType, settings.sourceType, settings.sourceTarget, settings.sourceId);
return arr;
}
在控制台中产生这个:
start
undefined
[]
0:
barType:"master"
bgcolor:"#12181f"
curVal:160
data:
all_goals:160
cost_hours:160
cost_hours_spent:0
cost_money:31610
cost_money_owned:0
parentObj:"progBar"
progress_goals:5
recurring:"no"
wanted_timer:"2018-03-26 05:19:33"
__proto__:Object
fill:"#255f6f"
height:59
maxVal:5
maxWidth:168
sectionHeight:59
stroke:"#7b9dac"
text:"3%"
textColor:"#dee5ed"
textOpt:"percent"
width:200
x:33
y:81
__proto__:Object
height:100
text:"omanko"
length:1
__proto__:Array(0)
stop
我确实认出了这里的对象,但据我所知,它不是来自全局污染 - console.log(window.arr) 说没有名为 arr 的全局变量,我还没有修改原型。
当然,这不应该影响新的数组声明吗?