legend legend - 1 month ago 4
Javascript Question

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;
}
})


What Console Showed

the object

Answer

Try

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

I think your Object is actually in an array;