【问题标题】:Why I can not initialize my variable in jQuery?为什么我不能在 jQuery 中初始化我的变量?
【发布时间】:2015-05-16 21:16:53
【问题描述】:

我的应用卡住了。我想使用函数之类的变量创建自己的reports.js 文件,但我无法对其进行初始化。我正在复制模板中已经完成的其他模板,但我不明白为什么我的不起作用。

这是我称之为头部的部分。

$(document).ready(function(){
   "use strict";

   App.init(); // Init layout and core plugins
   Plugins.init(); // Init all plugins
   FormComponents.init(); // Init all form-specific plugins

   Reports.init();

   $('#StartDate').pickadate({
     formatSubmit: 'yyyy/mm/dd',
     hiddenName: true
   });
});

当我运行网站时,控制台显示 TypeError: Reports.init 不是函数

现在我要分享 report.js 文件:

var Reports = function () {

   "use strict";

   var form = $('form').attr('id');
   alert(form);
   /* * * * * * * * * * * * * * *
    * Employee Clock-In Clock-Out
    * * * * * * * * * * * * * * */
   var EmployeeClocks = function(form) {
      // will do something
   }

   return {
       // main function to initiate reports
       init: function () {
          // EmployeeClocks();
       },
   };
}

【问题讨论】:

  • 可能希望将“使用严格”移到 $(document) 上方。准备好。如果将它放在脚本的顶部,则不需要在每个函数中调用它

标签: javascript jquery function initialization init


【解决方案1】:

添加()。你的Reports 现在是一个函数,而不是一个类。但是这个函数返回你想要的东西。

var Reports = function () { ... }()

【讨论】:

  • 谢谢伙计!我没有在其他脚本中注意到这一点!
【解决方案2】:

$(document).ready(function(){
   "use strict";

   App.init(); // Init layout and core plugins
   Plugins.init(); // Init all plugins
   FormComponents.init(); // Init all form-specific plugins

   // Reports.init();
   var reports = new Reports();
   reports.init();

   $('#StartDate').pickadate({
     formatSubmit: 'yyyy/mm/dd',
     hiddenName: true
   });
});

【讨论】:

  • 你在report.js中定义了一个函数构造函数
  • 感谢您的帮助,我选择另一个只是因为代码少... ;)
猜你喜欢
  • 2011-05-26
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 2020-05-31
  • 2012-07-12
  • 2012-01-07
  • 2018-06-11
  • 1970-01-01
相关资源
最近更新 更多