Ob wrote:
> Alright gurus here is what i am trying to do:
>
> Test = Class.create({
> options: {
> mystr: "hello",
> callback: Prototype.EmptyFunction
> },
> initialize: function(opt)
> {
>
> Object.extend(this.options, opt);
>
> }
> });
>
> var x = new Test({mystr: "variable x", callback: function()
> { alert("x"); } });
> var y = new Test({mystr: "variable y with no callback"});
>
> now when i do :
>
> x.options.callback(); // i get alert -- which is what i expect
>
> but when i do:
>
> y.options.callback(); //it again pops up an alert.. which isn't
> suppose to happen..
>
> any ideas what am i doing wrong?
>
>
You need to clone this.options:
Object.extend(Object.clone(this.options), opt);
All objects are passed by reference so each new instance will use the
same object assigned to Test.prototype.options. Javascript has no
concept of instance variable defaults. The other option is to define
this.options inside initialize instead of at Test.prototype.options.
- Ken Snyder
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---