views:

807

answers:

3

Hi

I'm writing a basic class using prototype.js in which some class vars are set when the class is initialised - the problem is that these variables aren't available to other methods within the class.

var Session = Class.create({
initialize: function(){
 // define defaults
 this.source = '';
},
shout: function(){
 alert(this.source);
}});

I know it's something to do with scope and I'm sure it's a fairly basic issue - all help appreciated!

Thanks, Adam

+1  A: 

I tested Your code. It works as far as I can tell. Maybe setting the variable to the empty string is throwing you off?

toby
+1  A: 

looks right... and it works for me.

document.observe('dom:loaded', function() {
    var s = new Session();
    s.shout();
});
ob
+1  A: 

What error are you getting? I've tried a number of permutations and can't reproduce anything that looks like the problem you are reporting.

MarkusQ