繁体   English   中英

如何使用OOP javascript进行此操作?

[英]How to use THIS with OOP javascript?

我是JavaScript的新手,只是试着像PHP一样做一些东西。 在下面的例子中,我不能使用THIS关键字一般调用类Screen的getMyWeekDay()方法,我在原型函数中要做的就是调用我的“Screen”类的特定实例(即我的实例)叫做“屏幕”)哪种破坏目的有点......

我想我刚刚在设计中犯了一个新手错误?

//Our screen object
function Screen() {}

//update the clock element on the screen
Screen.prototype.updateClock = function(date) {
    //do some stuff
    $('#dateContainer').html(screen.getMyWeekDay(date)+' '+date);
    //Here I would prefer    $('#dateContainer').html(this.getMyWeekDay(date)+' '+date);
}

//return the day name in swedish
Screen.prototype.getMyWeekDay = function(d) {
    var d=new Date(d);
    var weekday=new Array(7);
    weekday[0]="Söndag";
    weekday[1]="Måndag";
    weekday[2]="Tisdag";
    weekday[3]="Onsdag";
    weekday[4]="Torsdag";
    weekday[5]="Fredag";
    weekday[6]="Lördag";
    var n = weekday[d.getDay()];
    return n;
}

screen = new Screen();

//use the new instance (screen) of class Screen

- 更新 -

我意识到我的问题可能超出了我分享的代码,使用了建议的解决方案。

这是我的整个代码,我只是试图为您节省一些我认为不必要的阅读...

Screen.prototype.updateClock = function() {

    var jqxhr = $.get('{{ path('getClock') }}')
      .success(function(data) { 
            time = data.substring(data.indexOf("|")+1, data.indexOf("|")+6);
            date = data.substring(0, data.indexOf("|"));

            $('#timeContainer').html(time);          

            try{
                //see if template has its own function for date rendering...
                template_date_format(getMyWeekDay(date),date);
            }
            catch(e) {
                //standard fallback if not
                $('#dateContainer').html(this.getMyWeekDay(date)+' '+date);
            }       
       })
      .error(function() {
            console.log('there was an error fetching clock data');
      });        
            setTimeout(function() {
              updateClock();
            }, 15000);          

}
Screen.prototype.updateClock = function() {

var oopThis = this;

var jqxhr = $.get('{{ path('getClock') }}')
    .success(function(data) { 

        // the log line below will return an object which contains the ajax calls options
        console.log(this);

        time = data.substring(data.indexOf("|")+1, data.indexOf("|")+6);
        date = data.substring(0, data.indexOf("|"));

        $('#timeContainer').html(time);          

        try {
            //see if template has its own function for date rendering...
            template_date_format(getMyWeekDay(date),date);
        } catch(e) {
            //standard fallback if not
            $('#dateContainer').html(oopThis.getMyWeekDay(date)+' '+date);
        }    
    })
    .error(function() {
        console.log('there was an error fetching clock data');
    });        

    setTimeout(function() {
        oopThis.updateClock();
    }, 15000);
}

好。 基于上面的代码, thisajax/get/post函数的上下文中引用包含其选项的ajax对象。

BTW,常见的惯例是做var self = this;

暂无
暂无

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

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