0

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
  • Thanks guys - my (non)working copy was a bit more full than the one above, which I hadn't realised works. I'll go back and start from here! Thanks for all your replies Commented Apr 2, 2009 at 9:04

3 Answers 3

1

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

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

Comments

1

looks right... and it works for me.

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

Comments

1

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.

Comments

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.