From: Robert R. <rra...@ya...> - 2000-12-11 20:55:08
|
I was wondering the point of this line when inheriting objects: MyObject.prototype = new DynObject(); Wouldn't it be better to say: MyObject.prototype = DynObject.prototype Because in your MyObject constructor you will be saying: this.DynObject = DynObject; this.DynObject(); so all of the properties in the DynObject constructor will be initialized, and the MyObject.prototype = DynObject.prototype line will set all of the methods equal to the MyObject. Using the first method you are always calling the constructor an extra time that is not needed (or is it?). -- // Robert Rainwater |
From: Scott A. L. <sc...@sc...> - 2000-12-11 23:01:39
|
> Wouldn't it be better to say: > MyObject.prototype = DynObject.prototype I think this would cause problems when more than one object inherits from the DynObject. Using "new" ensures that the widget prototype is unique and won't be shared with other widgets that also use DynObject as their parent class. For example: let's say you have two widgets that inherit from DynObject, Widget A and Widget B: WidgetA.prototype = DynObject.protoype WidgetB.prototype = DynObject.protoype If Widget A overwrites a method of DynObject, it will also overwrite it for Widget B, because they share the prototype of DynObject: WidgetA.prototype.setSize = function(){ /*whatever*/} <-- this will overwrite the method for WidgetB, too. So I would think that using "new DynObject" instead would keep these unique and seperate. However, you brought up a very good point earlier in that using "new DynLayer" to create the prototypes for widgets creates an instance of DynLayer that ends up in the unassigned array: WidgetA.prototype = new DynLayer(); <-- this is a new instance, and ends up in unassigned[] This is probably not fatal, though, as it never gets assigned to a document. But it happens for every widget that uses this method. -- scott andrew lepera ----------------------------------- web stuff: www.scottandrew.com music stuff: www.walkingbirds.com |
From: Brandon M. <bnd...@ho...> - 2000-12-11 23:01:42
|
I think it's always better not to say MyObject.prototype=new DynObject Reasoning: We shouldn't want instance variables to be prototype variables. All variables initialized within the DynObject constructor will become part of the prototype. Why do we want this? Variables should be initialized at the time the instance is created, no sooner. I remember having a problem when I was trying to use the prototype to make sure a variable was available at the time the instance was created. I found that the variable space was shared. Especially if a varaible is an object. This could cause undefined behavior of other widgets downline. I like the idea of setting the prototypes equal.. it seems more direct, and doesn't set instances of objects as prototypes of another. ----- Original Message ----- From: "Robert Rainwater" <rra...@ya...> To: "DynAPI Development List" <dyn...@li...> Sent: Monday, December 11, 2000 3:57 PM Subject: [Dynapi-Dev] Inheritance > > I was wondering the point of this line when inheriting objects: > > MyObject.prototype = new DynObject(); > > Wouldn't it be better to say: > MyObject.prototype = DynObject.prototype > > Because in your MyObject constructor you will be saying: > this.DynObject = DynObject; > this.DynObject(); > > so all of the properties in the DynObject constructor will be > initialized, and the MyObject.prototype = DynObject.prototype line > will set all of the methods equal to the MyObject. Using the first > method you are always calling the constructor an extra time that is > not needed (or is it?). > > -- > // Robert Rainwater > > > _______________________________________________ > Dynapi-Dev mailing list > Dyn...@li... > http://lists.sourceforge.net/mailman/listinfo/dynapi-dev |
From: Joachim L. <lu...@ho...> - 2000-12-12 01:03:05
|
At 2000-12-12 00:03 , you wrote: >I think it's always better not to say MyObject.prototype=new DynObject (That makes me understand SuperClass better...) >Reasoning: We shouldn't want instance variables to be prototype variables. >All variables initialized within the DynObject constructor will become part >of the prototype. >Why do we want this? Variables should be initialized at the time the >instance is created, no sooner. >I remember having a problem when I was trying to use the prototype to make >sure a variable was available at the time the instance was created. I found >that the variable space was shared. Especially if a varaible is an object. >This could cause undefined behavior of other widgets downline. Well, that's the problem with the prototype-based way. Because the prototype is an object that is used when looking up a property/method that the "class" doesn't implement itself, setting unique properties should be done in the creator function instead, with a call to the creator function for the immediate superclass. For example the ListItem class: function ListItem(text,value) { this.superClass = Label; this.superClass(text); this.value = value; ... } ListItem.prototype = new Label(); Perfect, except for the fact that one object is wasted to set the prototype. But the prototype chain is right! >I like the idea of setting the prototypes equal.. it seems more direct, and >doesn't set instances of objects as prototypes of another. But that's the way inheritance in JavaScript works... And how would it work if you want to make a subclass of a subclass? And call a superclass' method? /Lunna |
From: Simon D. M. <di...@bi...> - 2000-12-12 13:10:17
|
> For example the ListItem class: > > function ListItem(text,value) > { > this.superClass = Label; > this.superClass(text); > this.value = value; > ... > } > ListItem.prototype = new Label(); > > Perfect, except for the fact that one object is wasted to set the > prototype. There's always the option of leaving out the last line if the extra object is a problem. It would only mean that if you made changes to Label.prototype at runtime, it wouldn't affect ListItem.prototype That could either be a good thing or a bad thing, depending what you want. I'm not sure what the problem with the extra objects is, but you could make your extra objects identifiable by setting the ID: ListItem.prototype = new Label('protoListItem'); SD |
From: Joachim L. <lu...@ho...> - 2000-12-12 17:28:14
|
At 2000-12-12 14:01 , you wrote: >> For example the ListItem class: >> >> function ListItem(text,value) >> { >> this.superClass = Label; >> this.superClass(text); >> this.value = value; >> ... >> } >> ListItem.prototype = new Label(); >> >> Perfect, except for the fact that one object is wasted to set the >> prototype. > >There's always the option of leaving out the last line if the extra >object is a problem. For me it's no problem, as long as I keep in mind the implications of it. >It would only mean that if you made changes to Label.prototype at >runtime, it wouldn't affect ListItem.prototype > >That could either be a good thing or a bad thing, depending what >you want. For me personally this would be a very bad thing... I depend on the classes be subclassable to extend the functionality, among other things. >I'm not sure what the problem with the extra objects is, but you >could make your extra objects identifiable by setting the ID: > > ListItem.prototype = new Label('protoListItem'); > >SD The whole point I was trying to make is that the prototype is A Good Thing (well...) and using object creators as constructors make the JavaScript language in this way even more object-oriented. It's true there is a (small) performance penalty but the added functionality is well worth it in my opinion. The biggest drawback IMHO with JS for now is the lack of private/public/protected properties and methods. Instance variables can be kludged however. Even though the next revision of the language will have classes (finally!) they can't be used in cross-browser environments. /Lunna |
From: Simon D. M. <di...@bi...> - 2000-12-11 23:20:28
|
> I was wondering the point of this line when inheriting objects: > > MyObject.prototype = new DynObject(); It's so that any changes to DynObject.prototype at runtime will be reflected in MyObject.prototype > Wouldn't it be better to say: > MyObject.prototype = DynObject.prototype I guess this would only create a pointer, so that changes to MyObject.prototype would affect DynObject.prototype SD |
From: Brandon M. <bnd...@ho...> - 2000-12-12 00:34:37
|
>> Wouldn't it be better to say: >> MyObject.prototype = DynObject.prototype >I guess this would only create a pointer, so that changes to >MyObject.prototype would affect DynObject.prototype Very good point.. so should we then loop through the prototypes?.. Assigning the individual functions, but not the entire prototype? Do this to avoid setting variables or objects which would then be shared by all instances of the subclass. function Label() { this.theLayer=new DynLayer(); } Label.prototype=new DynObject(); Then..... function Button() { this.theLayer.addEventListner(Button.mouseEvents); // More stuff here too. } Button.mouseEvents=...blah blah blah.... Button.prototype=new Label(); // Cause a button is just a label with events. Now.. guess what? All instances of Button will share the same DynLayer reference. Is this what is intended? I don't think so. Now.. don't get on my case that I used Dan's original widget model.. it's just a practical example. Any object in place of the "theLayer" object would have the same problems. -----Original Message----- From: dyn...@li... [mailto:dyn...@li...]On Behalf Of Simon Dicon Montford Sent: Monday, December 11, 2000 6:12 PM To: dyn...@li... Subject: RE: [Dynapi-Dev] Inheritance > I was wondering the point of this line when inheriting objects: > > MyObject.prototype = new DynObject(); It's so that any changes to DynObject.prototype at runtime will be reflected in MyObject.prototype > Wouldn't it be better to say: > MyObject.prototype = DynObject.prototype I guess this would only create a pointer, so that changes to MyObject.prototype would affect DynObject.prototype SD _______________________________________________ Dynapi-Dev mailing list Dyn...@li... http://lists.sourceforge.net/mailman/listinfo/dynapi-dev |