I have problem when copy object from a function in JavaScript.
I have script :
JavaScript
var _img;
fabric.Image.fromURL('phone.png', function(img){
img.setWidth(320);
img.setHeight(615);
img.setLeft(0);
img.setTop(0);
canvas.insertAt(img, 0);
img.set('evented', false);
_img = Object.assign({}, img);
console.log(_img);
});
console.log(_img);
the problem is, 2nd console.log
result is undefined
. I want to copy object from img
to _img
for reason. How to do that in a proper way ?.
Thank you.
Edit : Thank you. I found solution from here : How do I return the response from an asynchronous call? . Now i can get return value from async call, here the code :
function addImage(callback){
fabric.Image.fromURL('phone.png', function(img){
callback (img);
});
}
var _img;
addImage(function(result){
_img = result;
console.log(_img); // result is ok
})
console.log(_img); // result still undefined
But, i still can pass the result
to _img
object. What should I do ?