Javascript 检测对象构造函数的onload
我想知道是否有一种方法可以检测对象何时加载 诸如此类:Javascript 检测对象构造函数的onload,javascript,object,onload,Javascript,Object,Onload,我想知道是否有一种方法可以检测对象何时加载 诸如此类: 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 obj
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!".
我知道我可以将代码放在属性之后,但我确实需要将onload“存储”为属性之一。只需使用此代码,无论是否初始化了
Square
object1.hasOwnProperty('width');
如果Square
已加载/初始化,则返回true,否则返回false
或
如果你想用这种方法,那么就使用
var object1 = new Square();
object1.onload(); //call this method for check loaded or not
您必须在构造函数中显式调用
onload
方法:this.onload()
Wow。通过阅读你的评论,我意识到答案是多么简单。我完全没有想过。谢谢