Question

I am wondering if there is a way of detecting when an object is loaded.

Something like that:

var Square = function(){
    this.width = 100;
    this.height = 100;

    //Onload would go here.
    this.onload = function(){
        console.log("Square loaded successfully!");
    }
}

//Creating a new square.
var object1 = new Square();

//When the object1 i loaded, the console log returns: "Square loaded succesfully!".

I know i can just put my code after my properties, but i really need the onload to be "stored" as one of the properties.

Was it helpful?

Solution

Simply use this code that Square is initialized or not

object1.hasOwnProperty('width');

return true if Square has been loaded/initialized otherwise return false.

OR

If you want to do it by method then use.

var object1 = new Square();
object1.onload(); //call this method for check loaded or not
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top