当前位置: 动力学知识库 > 问答 > 编程问答 >

Javascript class variable scope using prototype

问题描述:

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

网友答案:

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

网友答案:

looks right... and it works for me.

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

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: