简体   繁体   English

将父方法包装在javascript的私有函数中时如何调用方法

[英]How to call method when I wrapped the parent method in a private function in javascript

I'm not sure I worded my question correctly but anyhow... 我不确定我的措词是否正确,但是无论如何...

I'm using iscroll and wrapped the iScroll init call in a private function for my use later. 我正在使用iscroll,并将iScroll init调用包装在私有函数中,以备后用。 this is what I have: 这就是我所拥有的:

 Namespace.iscroll =  function () {
        var myScroll;
      myScroll = new iScroll('mainContent-wrap', { 
          hScrollbar: false 
       });

    document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
    document.addEventListener('DOMContentLoaded', Namespace.iscroll, false);
};

I call iscroll on any page I need by using Namespace.iscroll(); 我可以使用Namespace.iscroll();在需要的任何页面上调用iscroll Namespace.iscroll();

My question is, now I want to refresh iScroll using myScroll.refresh(); 我的问题是,现在我想使用myScroll.refresh();刷新iScroll myScroll.refresh(); , or using the setTimeout function as descriobed in the iScroll documentation. ,或使用iScroll文档中介绍的setTimeout函数。

Since I wrapped the original init call in a private function how to I run the refresh, and other methods (like destroy) on it? 由于我将原始init调用包装在私有函数中,因此该如何运行刷新以及其他方法(例如destroy)?

If you're trying to get access to the myScroll variable after the iscroll function has been run, the answer is you can't. 如果在运行iscroll函数之后尝试访问myScroll变量,答案是您不能。 It's gone at that point. 到此为止。 You will have to save it somewhere else so you can get to it. 您将不得不将其保存在其他地方,以便可以使用它。 One idea is to put it on the same object as iscroll like this: 一种想法是将其与iscroll放在同一对象上,如下所示:

Namespace.iscroll = function () {
    Namespace.myScroll = new iScroll('mainContent-wrap', {
        hScrollbar: false
    });

    document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
    document.addEventListener('DOMContentLoaded', Pelau.iscroll, false);
};

You can then do a refesh like this: 然后,您可以像这样进行刷新:

Namespace.myScroll.refresh();

The key is that you store myScroll in some persistent and public location that isn't a temporary local variable. 关键是您将myScroll存储在不是临时局部变量的某些持久公共位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM