I am new to OOP in JavaScript, just trying to do some stuff the same way I would i PHP. In the example below I can't use the THIS keyword to generically call the method getMyWeekDay() of the class Screen, what I have to do in my prototype function is to call a specific instance of my "Screen" class (namely my instance called "screen") which kind of ruins the purpose a little bit...
I guess I just made a newbie mistake in my design?
//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
-- UPDATE --
I realize my problem might have been outside of the code I shared, using the solution suggested worked though.
Here is my entire code, I was just trying to spare you a bunch of what I considered unneccessary reading...
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);
}