douzhongjiu2263 2015-02-08 04:59
浏览 3
已采纳

JavaScript - 相当于PHP类自我::

So I've been searching on this because it's giving me a headache not being able to achieve this... And seems like there is no solution the way I expected to be.

Let's assume we have a sample PHP class:

<?php 

class SampleClass {

    function doA() {
        echo 'Hello world';
    }

    function doB() {

        $sampleArray = ['Hey', 'You'];

        foreach( $sampleArray as $key => $value ) {

            self::doA();

        }

    }

}

?>

This code will echo Hello World two times because of the foreach loop. And thats good.

Now I have this script:

function SectionHandler($appWrap) {

 this.appWrap = $appWrap;

 this.loading = function() {
    this.appWrap.addClass('section-loading').html("<i class='fa fa-refresh fa-2x fa-spin'></i>");
 }

 this.setError = function($info) {
    this.appWrap.addClass('section-error').html($info);
 }

 this.load = function($section) {

  var $fileCheck = $.get($section).success(function(){
    self.loading();
    self.load($section);
  }).fail(function(){
    self.setError('No se pudo cargar los datos especificados desde ' + $section + ' ;');
  });

 }

}

I believe that this.loading() is no longer pointing to the main function because of the $.get().success() function. I do not know the real reason.

The problem is that none of this.loading(), this.load(), this.setError() is working.

So, how I do to point to the main function like I were using PHP self:: function.?

-- EDIT --

As @Eclecticist pointed out the line this.load($section); will execute untill it fails or gets an error, I change the whole check to this:

var $fileCheck = $.get($section).done(function(response){
 self.appWrap.html(response);
}).fail(function(){
 self.setError('No se pudo cargar los datos especificados desde ' + $section);
});
  • 写回答

3条回答 默认 最新

  • dpik71879 2015-02-08 05:05
    关注

    Try this:

    function SectionHandler($appWrap) {
        var that = this;
    
        this.appWrap = $appWrap;
        this.loading = function() {
            that.appWrap.addClass('section-loading').html("<i class='fa fa-refresh fa-2x fa-spin'></i>");
        }
    
        this.setError = function($info) {
            that.appWrap.addClass('section-error').html($info);
        }
    
        this.load = function($section) {
            var $fileCheck = $.get($section).success(function(){
                that.loading();
                that.load($section);
            }).fail(function(){
                that.setError('No se pudo cargar los datos especificados desde /[ ' + $section + ' ]\ ;');
            });
        }    
    }
    

    The problem is the value of this changes once you're inside of a function. You can address it by declaring a variable within your main function, that, and reference it later on.

    --edit--

    I want to note that the line that.load($section); will recursively call SectionHandler.load() until the AJAX request either fails, or until you get a stackoverflow error.

    And yes, I've been hoping something came up where I could reference that error :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?