1

Quick question, why does my reference to weekdays inside change_date() give weekdays is undefined error in Firebug?

I also tried this.weekdays, same.

How do I correct this?

var timesheet_common = {

    weekdays : ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],

    change_date: function() {
        $('#text_input').val( weekdays[(new Date()).getDay()] );
    }

};
9
  • Maybe you changed the wrong weekdays. Only the weekdays inside the function should be this.weekdays. Commented Jul 4, 2010 at 16:56
  • How are you calling change_date? If you are attaching it to an event handler, the this keyword may refer to the object that is firing the event. Commented Jul 4, 2010 at 17:08
  • Alex M., you're right. I'm using $('#selected_date').datepicker({'onSelect':this.change_date}); Commented Jul 4, 2010 at 17:24
  • I'm wondering why the this.change_date works, but this.weekdays doesn't. Commented Jul 4, 2010 at 17:25
  • casablanca, yes I did $('#text_input').val( this.weekdays[(new Date()).getDay()] ); Commented Jul 4, 2010 at 17:26

4 Answers 4

1

Use this.weekdays because it's an object.

Edit: I tried with this and it worked for me.

Sign up to request clarification or add additional context in comments.

4 Comments

i get 'this.weekdays is undefined'
run in Firebug - var timesheet_common = { weekdays : ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],change_date: function() {console.log(this.weekdays);}};timesheet_common.change_date();
The exact code above, when run in Firebug says undefined? Hmm, that sounds like there's something wrong with your Firebug then :) The function context of the anonymous function assigned to the change_date property in the given code is the object literal to which the property belongs. What does it say if you replace this.weekdays with this?
@Obay Don't confuse the return value with the log message (ie in Chrome Dev Tools). It definitely works.
1

In JavaScript the function is not associated with its model. You might do sth like this:

var timesheet_common = (function(){

    var weekdays = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];

    var change_date = function() {
        $('#text_input').val( weekdays[(new Date()).getDay()] );
    };

    return { weekdays: weekdays, change_date: change_date }

})();

Comments

0

There are two problems with your code:

  1. Inside change_date() you should refer to weekdays as this.weekdays, because they're both defined in the same object.

  2. When you use change_date() as a click handler, you must use $.proxy(timesheet_common, 'change_date') instead; doing this makes sure change_date() is called within the context of timesheet_common rather than the clicked element.

Comments

0
function TimesheetCommon(){
    this.weekdays = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
}
TimesheetCommon.prototype.change_date = function(){
     $('#text_input').val( this.weekdays[(new Date()).getDay()] );
}

var timesheet_common = new TimesheetCommon();

2 Comments

wow i didn't know you could make objects like that in javascript. what's happening here? how come you can already call a new on TimesheetCommon? which part of that code turned TimesheetCommon into an object that is new-able?
Every function creates an object when called with new. Add methods to the prototype property as shown above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.