javascript - 如何将 THIS 与 OOP javascript 一起使用?

标签 javascript oop

我是 JavaScript 中 OOP 的新手,只是想像我在 PHP 中那样做一些事情。在下面的示例中,我不能使用 THIS 关键字来一般调用 Screen 类的方法 getMyWeekDay(),我在原型(prototype)函数中要做的是调用我的“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 对象。

顺便说一句,通常的约定是 var self = this;

关于javascript - 如何将 THIS 与 OOP javascript 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920928/

相关文章:

javascript - typescript 错误 : Cannot redeclare block-scoped variable 'fetch'

javascript - 单击带有 flash 的 iframe

javascript - 使用 SVG 字符串构建嵌套 SVG 的 LitElement 渲染问题

javascript - 如何在没有 ctx.bezierCurveTo 的情况下使用 native Javascript 代码绘制平滑曲线?

Matlab复制构造函数

javascript - spring mvc加载JS文件

c++ - 对象切片会破坏封装吗?

javascript - 使用 Handlebars 的 Backbone 应用程序的一般结构

javascript - 在同一个类中调用 EventEmitter 两次?

oop - 类只有静态字段和方法是不好的做法吗?