jQuery技术:从class级发送数据?

我正在研究延期和自定义事件。 我不确定哪种方法适合我的应用程序。

我有一个叫另一个class级的class级。 在这个类中,用户可以将文件拖到窗口上。

拖动文件后,我希望将有关文件的详细信息发送到我的主类。

我已经考虑过使用延迟,但是用户需要反复拖动文件,而且根据我的理解,这只能使用一次。

所以在我的主要课程中我:

this.dropBox = new DropBox(); 

然后在DropBox类中我有:

 $(window).on('drop', this.drop); 

但是我应该在我的’drop’方法中加入什么呢。 每当有什么东西被丢弃时,我希望“提醒”我的主要课程并采取行动。 我怎么能’听’这个活动。 我应该使用延期,自定义事件还是其他什么?

    通常有两种选择:

    代表

    委托应该实现某个“接口”,一组处理某些事件的函数。

     function DropBox(delegate) { this.delegate = delegate; $(window).on('drop', $.proxy(this, 'drop')); } DropBox.prototype.drop = function(e) { // do stuff with event this.delegate.drop(e); } // inside main instance this.dropBox = new DropBox(this); // delegate interface this.drop = function(e) { // handle file drop }; 

    打回来

    如果委托只需要一个函数,您也可以使用回调:

     function DropBox(dropEventHandler) { this.dropEventHandler = dropEventHandler; $(window).on('drop', this.drop); } DropBox.prototype.drop = function(e) { this.dropEventHandler(e); }; // inside main instance var self = this; this.dropBox = new DropBox(function(e) { // handle file drop // use 'self' to reference this instance }); 

    为什么不直接回拨DropBox?

    好吧,就像主类中的这段代码一样:

     this.dropBox = new DropBox(function(fileInfo) { // this code can be executed by the DropBox Object multiple times! }); 

    而DropBox:

     window.DropBox = function(callback) { this.userHasDroppedFiles = function(fileinfo) { // do stuff callback(fileinfo); // give the fileinfo back with the callback! } } 

    此外,JavaScript中没有类! 你只有对象,你可以使用与原型相结合的构造函数来生成类似行为的类,但你实际上永远不会有类似Java,C#或类似语言的类。 始终牢记这一点。 一些JS框架在主要JS可能性之上构建自己的类层,然后您可能拥有Framework类,但也从不使用 本机 JavaScript类,因为本机JavaScript类不存在!

      以上就是jQuery教程分享从class级发送数据?相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/541411.html

      (0)
      上一篇 2021年1月3日
      下一篇 2021年1月3日

      精彩推荐