简体   繁体   中英

Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined

I am sure that the object that I console had that value. The code listed as follow.

addLoadEvent(function(){
    console.log(toObj(1));          //shows an object;
    console.log(toObj(1).name);     //shows undefined;
    function toObj(name){
        var source_json = getCookie(name);
        return JSON.parse(source_json);
    }
    function getCookie(name){
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
        else
        return null;
    }
})

控制台显示了什么

物体

Try

console.log(toObj(1)[0].name);

I think your Object is actually in an array;

try this one to see all properties of the object:

for(var key in toObj(1)){
  console.log(key+': '+toObj(1)[key]);
}

You can also try toObj(1)['name'] to get the name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM