iporter wrote:
> ...var myClass = Class.create({
> options:{},
> value: false,
> initialize: function(options) {
> this.options.value = options.value;
> this.value = options.value;
> },
> myAlert:function() {
> alert(this.options.value + ' : ' + this.value);
> }
> });
>
> var classObj1 = new myClass({value:1});
> classObj1.myAlert();
> var classObj2 = new myClass({value:2});
> classObj1.myAlert();
>
>
What you're missing is the fact that myClass.prototype.options will be
shared across all instances the same way myClass.options would be. In
JS, objects (and arrays) are always passed by reference. This class
construction behavior is quite different than the behavior of true OO
languages. In JS, it is best to always initialize instance properties
inside of functions. Try the code below.
- Ken Snyder
var myClass = Class.create({
initialize: function(options) {
this.options = {};
this.value = false;
this.options.value = options.value;
this.value = options.value;
},
myAlert: function() {
alert(this.options.value + ' : ' + this.value);
}
});
var classObj1 = new myClass({value:1});
classObj1.myAlert(); // 1 : 1
var classObj2 = new myClass({value:2});
classObj1.myAlert(); // 1 : 1
classObj2.myAlert(); // 2 : 2
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---