Nope, I still don't have it. In the below code, the behaviours object
is shared between all the popup objects :0( If I declare it in the
Popup() function, it works properly - but I want to declare it in
complexDOM, since all complexDOM's will have behaviours. Any
pointers?
function Behaviours () {
this.array = new Array();
}
Behaviours.prototype.push(el, event, method) = function() {
this.array.push({el, event, method});
}
function complexDOM () {
this.behaviours = new Behaviours();
}
function Popup () {
this.options...
}
Popup.prototype = new complexDOM();
I also found that I was unable to use the below - when trying to loop
through this in the list function, this wasn't an array:
function Behaviours () {}
Behaviours.prototype = new Array();
Behaviours.prototype.list = function() {
this.each(function(behaviour) {...});
}
Cheers.
On Jan 19, 12:27 pm, iporter <[EMAIL PROTECTED]> wrote:
> Nope - I got it wrong, sorry! Replace Person(name); in the subclasses
> with Person.call(this, name);
>
> On Jan 19, 11:44 am, iporter <[EMAIL PROTECTED]> wrote:
>
> > Ok, I think I got it (if the indentation's funky, it's just so I can
> > collapse functions and they line up, so I still get a sense of where
> > classes start and end, as I had with the Class.create method):
>
> > function Behaviours () {
> > this.array = [];
> > }
> > Behaviours.prototype.push = function
> > (behaviour) {
> > this.array.push(behaviour);
> > }
> > Behaviours.prototype.list = function () {
> >
> > $A(this.array).each(function(behaviour) {
> > alert(behaviour);
> > });
> > }
>
> > function Person(name) {
> > this.name = name;
> > this.behaviours = new Behaviours();
> > }
>
> > function Man (name) {
> > Person(name);
> > this.behaviours.push('eat');
> > }
> > Man.prototype = new Person();
>
> > function Woman (name) {
> > Person(name);
> > this.behaviours.push('talk');
> > }
> > Woman.prototype = new Person();
>
> > On Jan 19, 11:27 am, iporter <[EMAIL PROTECTED]> wrote:
>
> > > Rob, I think I'm going to use the class.prototype.method syntax. I'm
> > > pretty sure it's still more verbose, but I want to use the same class
> > > definitions in the same .js file on the client and server sides, and
> > > it seems I can't if the class is a global variable, instead of a
> > > function
> > > (seehttp://groups.google.com/group/rubyonrails-spinoffs/browse_frm/thread...
> > > if interested).
>
> > > How does one acheive the effect of $super if not using Prototype's
> > > Class class?
>
> > > Cheers,
> > > Iain
>
> > > On Jan 18, 7:13 am, RobG <[EMAIL PROTECTED]> wrote:
>
> > > > 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
-~----------~----~----~----~------~----~------~--~---