On Jan 18, 2:30 am, iporter <[EMAIL PROTECTED]> wrote:
> In the below code, I expect two alerts '1 : 1' and then '1 : 1', but
> in reality I get '1 : 1' and '2 : 1'. For some reason, the
> declaration of the second object of class myClass alters the first
> object of the same class. However, it only alters
> 'this.options.value', and not 'this.value'. Can you tell me why this
> behaviour occurs, and how to resolve it?
>
> Cheers
>
> ---------------------------------------------------------------------------
> 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();
I think you are better off to understand what is actually happening.
Javascript does not have classes - it isn't an object oriented
language, it's object based. It uses prototypes for inheritance, not
classic OO inheritance.
While you can paper over the differences, they are still there. Two
features that often cause issues are the this keyword and closures,
which have combined here to cause an unexpected result.
To write your "class" (it's actually a javascript constructor, but
whatever) using plain javascript, you would write something like:
function MyClass(obj) {
this.value = obj.value;
}
MyClass.prototype.options = {};
MyClass.prototype.showValue = function(){
alert(this.options.value + ':' + this.value);
}
That's it. Note that this takes less code than using Class.create().
Now if you test this:
var x = new MyClass({value:1});
x.showValue(); // --> 1:1
var y = new MyClass({value:2});
x.showValue(); // --> 2:1
You get the result you didn't want - the options object is shared by
all instances of MyClass because it is a property of the constructor's
prototype. If you want each instance of MyClass to have its own
options object, you add it in the constructor:
function MyClass(obj) {
this.value = obj.value;
this.options = {};
this.options.value = this.value; // or obj.value;
}
MyClass.prototype.showValue = function(){
alert(this.options.value + ':' + this.value);
}
Now when you create some instances:
var x = new MyClass({value:1});
x.showValue(); // --> 1:1
var y = new MyClass({value:2});
x.showValue(); // --> 1:1
y.showValue(); // --> 2:2
Is that what you wanted? To me, the above is much simpler and clearer
than using Class.create, and is less code.
--
Rob
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---