Why is this object property undefined?

后端 未结 2 1643
Happy的楠姐
Happy的楠姐 2021-02-20 08:15

Consider the code below. The first console.log correctly logs the image, and you can see its properties in the image below. However, when I try logging one if its p

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-20 08:35

    I think the object keys have unprintable characters, such can be replicated like this:

    var obj = {};
    obj["E"+String.fromCharCode(15)] = new Array(15);
    
    console.log(obj);
    
    /*Object
    E: Array[15]
    __proto__: Object*/
    
    console.log(obj.E)
    
    //undefined
    
    console.log( obj["E"+String.fromCharCode(15)] )
    
    //[]
    

    Edit: you can see if this is the case for your object keys:

    var realKeys = [];
    
    for( var key in obj ) {
    realKeys.push( [].slice.call( key ).map( function(v){return v.charCodeAt(0);} ).join(" ") );
    }
    
    //["69 15"] (69 stands for the letter "E" and 15 was the unprintable character I added manually)
    

    Edit2: Since you can't do that I came up with another way to see if there are unprintable characters:

    Copypaste the key string like this: (go all the way as much as you can on both ends so you pick any invisible characters)

    Then dump your clipboard like this (Make sure you are using double quotes):

提交回复
热议问题