Object literal object builder error javascript

综合技术 2018-05-18 阅读原文

I am trying to organize my code using the object literal pattern, but I am getting an error: Uncaught ReferenceError: backToTop is not defined

Any ideas why?

Here's the code:

(function($){

  var Mk = {

    config: {},

    init: function(config) {
      backToTop();
      wow();
      progressBarAnimation();
      slabText();
    },

    backToTop: function() {
      $('body').append('
Back to Top
'); $(window).scroll(function () { if ($(this).scrollTop() != 0) { $('#toTop').fadeIn(); } else { $('#toTop').fadeOut(); } }); $('#toTop').click(function(){ $("html, body").animate({ scrollTop: 0 }, 600); return false; }); }, wow: function() { var wow = new WOW({ boxClass: 'wow', animateClass: 'animated', offset: 0, mobile: true, live: true }); wow.init(); }, progressBarAnimation: function() { $.each($('div.progress-bar'),function(){ $(this).css('width', $(this).attr('aria-valuetransitiongoal')+'%'); }); }, slabText:function() { $("h1.mklife").slabText({ "viewportBreakpoint":400 }); }, last:'' }; $(document).ready(Mk.init()); window.Mk = Mk; })(jQuery)
  • Edit: I had missed the actual function call parentheses on document ready: $(document).ready(Mk.init());

The functions backToTop
, wow
, progressBarAnimation
, slabText
are methods of the Mk object, to access them reference the Mk object

init: function(config) {
  Mk.backToTop();
  Mk.wow();
  Mk.progressBarAnimation();
  Mk.slabText();
},

or since init is also a method of the same object you can access the function with the this
keyword

init: function(config) {
  this.backToTop();
  this.wow();
  this.progressBarAnimation();
  this.slabText();
},
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

如何使用chrome浏览器进行js调试找出元素绑定的点击事件... 大家有没有遇到这样的一个问题,我们在分析一些大型电子商务平台的Web前端脚本时,想找到一个元素绑定的点击事件,并不是那么容易,因为有些前端脚本封装的比较隐蔽,甚至有些加密脚本,用传统的查找元素ID、或者页面源码方法去找,可能最后徒劳无功。下...
从 React 绑定 this,看 JS 语言发展和框架设计... 在 javascript 语言中,关于 this 这个关键字的行为一直以来困扰着一代又一代初级开发者。同时 this,也充分反应了 javascript 的诡异与灵活。 但是请别误会,这篇文章并不会对 this 的特征进行全...
Import multiple svgs into an xml with d3&perio... currently I'm fiddling with D3.js. It looks awesome!I was wondering if D3.js is suitable to draw multiple svg element...
Insert the row of the table and the Delete button ... I've got a form with a table with originally 1 row of input fields. Users can click the "New Row" button to get another ...
The Javascript code does not work overloaded I have 3 variables to track timespent in the site $_SESSION; //logged when user first enters the site $_SESSION; /...