[英]Self-Executing Functions on Instantiation in a Javascript Class
I have a class in Javascript based on this Base Class by Josh Gertzen. 我有一个基于Josh Gertzen的基本类的Javascript 类 。 I'd like the class to self-execute an 'initialize' function for adding event listeners when it's instantiated. 我希望类在实例化时自动执行一个“初始化”函数以添加事件侦听器。 Is this possible? 这可能吗?
This is in my main application script: 这是在我的主要应用程序脚本中:
var mainView = new MainView(model)
/*the line I want to remove but don't know how*/
mainView.initialize();
And this is the method setting up the listener inside of the class extension. 这是在类扩展内设置侦听器的方法。
initialize : function ()
{
$(document).on("Loading", this.addPreloader);
}
Any help would be really appreciated. 任何帮助将非常感激。 Thank you! 谢谢!
function MainView(someParameter){
this.property = null;
this.initialize = function(someParameter){
/* construction code */
this.property = 'test';
};
this.initialize();
};
var main = new MainView();
console.log(main.property);
This will help i think. 这将有助于我的思考。 You can also look at this SO Question for more information about javascript and constructors . 您也可以查看此SO问题以获取有关javascript和构造函数的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.