1

I have this erratic behavior: I have an object, I extend its property and initalize it. I do a callback to populate the property. Then, if I check the property outside the callback, it results as initialized; but if I check the whole object,also the property is updated.

Am I doing something wrong in updating my property?

Am I doing smtg conceptually wrong with local variables?

I update my property with this function:

function extend(obj, props) {
    for(var prop in props) {
        if(props.hasOwnProperty(prop)) {
            obj[prop] = props[prop];
        }
    }
};

And then:

graphics.node(function (node) {



                extend(node,{
                      thumbnail : {
                        source: '',
                        width: '',
                        height: ''
                      }

                    });

                // UI

                if (typeof node.data === 'undefined') {
                    var nameTopic = 'NOT YET MAPPED'; // YOU DONT NEED THIS INFO
                    var sourceId = 'ID NOT YET MAPPED' // YOU DONT NEED THIS INFO

                } else {
                    var nameTopic = node.data.name;
                    var sourceId = node.data.source;

                // I POPULATE MY PROPERTY WITH FOLLOWING FUNCTION: node.thumbnail...

                    fetchWikipediaDecorator(sourceId,function(data){


                        var infopage = data.query.pages[sourceId];

                        if (infopage.thumbnail) {
                           node.thumbnail.source = infopage.thumbnail.source;
                           node.thumbnail.width = infopage.thumbnail.width;
                           node.thumbnail.height = infopage.thumbnail.height;

                        }


                    });

                   // NOW ODD THING IS:
                   console.log('registro il source',node.thumbnail); 
                   // RESULT: {source:'',width:'',height:''}
                   // INSTEAD
                   console.log('registro il source', node);
                   // RESULT WITH UPDATED PROPERTIES FOR ALL THE OBJECT
                   // {...,thumbail{source:'http...',etc.}, ..}

                }

                // SO BASICALLY I CANT CALL THE PROPERTY..
user305883
  • 1,635
  • 2
  • 24
  • 48
  • I read the useful [http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron] but still it is not clear to me: 1. I am using a callback and once result is ready, it updates a property object (outside the object of the function): in this case, "node" object. 2. Not clear to me why the different result in calling the property: it is undefined if I call object.property (node.thumbnail), but updated within the whole object (node): so I don't understand if the issue is due to async and passing parameters from callback, or other. – user305883 Jul 27 '15 at 09:06

0 Answers0