JavaScript中的奇怪数组行为 [英] weird array behaviour in javascript

查看:66
本文介绍了JavaScript中的奇怪数组行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与其他语言相比,我知道javascript中的数组有点特殊,但是我并没有真正了解这种行为,或者这是怎么回事.

I know arrays in javascript are a bit special, compared to other languages, but I don't really get this behaviour, or what's going on here.

我想知道为什么会发生以及为什么我没有得到一个空数组:

I'd like to know why it happens and why I don't get an empty array:

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的全局变量,并且我还没有修改原型

I do reckognize the objects in here, but it's not from global pollution as far as I can tell - console.log(window.arr) says that there are no global variables named arr, and I haven't modified the prototype.

当然,那不应该影响新的数组声明吗?

Surely, that shouldn't effect a new array declaration anyway?

推荐答案

此行为不仅限于数组,任何对象在控制台中的行为都如此

This behaviour isn't limited to arrays, any object behaves this way in the console

您所看到的是控制台在所有说谎"给您的浏览器中的结果

What you are seeing is the result of console in all browsers "lying" to you

如果您console.log(anyobject)并在控制台中检查该对象,您将看到的是当前anyobject-不是执行console.log时的状态

if you console.log(anyobject) and inspect that object in the console, what you will see is current anyobject - not what it was when console.log was executed

var obj = {}
console.log(obj);
obj.test = 1;

var arr = [1];
console.log(arr);
arr.push(2);

现在,如果打开开发者控制台,请单击对象,您将看到test:1

Now, if you open the developer console, click on the Object, you'll see test:1

在控制台中查看数组-输出为[1] ...,但是,单击同时看到两个元素的数组

Look at the array in the console - it is output as [1] ... yet, click on the array you see both elements

注意:chrome开发者控制台至少暗示了它对您说谎的事实-蓝色i,如果您将鼠标悬停(或单击,无法回忆起,不要足够频繁地使用Chrome),您会:将会看到一条消息,提示显示的值为evaluated just now

Note: chrome developer console does at least hint at the fact that it's lying to you - there's a blue i, if you hover (or click, can't recall, don't use Chrome often enough) you'll see a message saying that the value shown is evaluated just now

这篇关于JavaScript中的奇怪数组行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆