I find it difficult to understand how to access object properties in Javascript. I have a function that returns an object, and I can see this object and all its properties when it registers on the console in Safari, but I cannot get property values for other functions. For example, an attempt to notify about one of the properties returns "undefined".
Function that generates the object
getProfile : function() {
FB.api('/me', function(response) {
facebook.profile.user_id = response.id;
facebook.profile.name = response.name;
facebook.profile.firstName = response.first_name;
facebook.profile.lastName = response.last_name;
facebook.profile.gender = response.gender;
});
FB.api('/me/photos', {limit: 8}, function(response) {
facebook.profile.numPhotos = response.data.length;
for (key in response.data) {
var photoUrl = response.data[key].source;
eval('facebook.profile.photo' + key + '= photoUrl');
}
});
return facebook.profile;
}
Trying to use this function in another script
function loadProfile() {
var profile = facebook.getProfile();
console.log (profile); alert (profile.name); }
source
share