|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Inheritance questionsI was reading the documentation regarding declaring classes in Dojo at
http://docs.dojocampus.org/dojo/declare I have some questions regarding inheritance. The documentation says that super constructors are called automatically. But how do you select which arguments are used to call that super constructor. You might want to call the super constructor with less arguments, or pass a parameter not given by the arguments in this constructor. Example: dojo.declare("Class1", null, { constructor: function(x){ this.x=x; } }); dojo.declare("Class2", Class1, { constructor: function(){ // I want to call Class1.super("value") } }); The other question I have is regarding this.inherited. Sometimes I would like to call a super method that is not the same as the method currently executing. And I would like to call it with different parameters than the arguments of the current method. Example: dojo.declare("Class1", null, { method1: function(a){ } }); dojo.declare("Class2", Class1, { method2: function(){ // I want to call Class1.super.method1("value") } }); Is this possible? |
|
|
Re: Inheritance questionsThe idea of the constructor is that ALL constructors are called with
the same parameters. You might be able to mess with it a bit using the preamble - I'm not sure how that would work. But the short answer is that changing stuff in the constructor could lead to unexpected consequences. The main reason being that it is "guaranteed" that the constructor is called, so many classes take that as a given - and rely upon that fact. You can, however call another method - just using the regular javascript calls. For example: dojo.declare("Class1", null, { method1: function(a){ } }); dojo.declare("Class2", Class1, { method2: function(){ Class1.prototype.method1.call(this, "My Custom Value"); } }); the "call" function is a function on a javascript function object that takes a scope, and then a comma-separated list of parameters to pass in to the function. That function is called within the given scope. However, in this case, I don't know why you don't just instead do: this.method1("My Custom Value") Since Class2 inherits from Class1, Class2 has that method already. -Nathan On Aug 17, 2009, at 2:45 AM, -Robert- wrote: > > I was reading the documentation regarding declaring classes in Dojo at > http://docs.dojocampus.org/dojo/declare > I have some questions regarding inheritance. > > The documentation says that super constructors are called > automatically. But > how do you select which arguments are used to call that super > constructor. > You might want to call the super constructor with less arguments, or > pass a > parameter not given by the arguments in this constructor. > Example: > > dojo.declare("Class1", null, { > constructor: function(x){ > this.x=x; > } > }); > > dojo.declare("Class2", Class1, { > constructor: function(){ > // I want to call Class1.super("value") > } > }); > > > The other question I have is regarding this.inherited. > Sometimes I would like to call a super method that is not the same > as the > method currently executing. > And I would like to call it with different parameters than the > arguments of > the current method. > > Example: > > dojo.declare("Class1", null, { > method1: function(a){ > } > }); > > dojo.declare("Class2", Class1, { > method2: function(){ > // I want to call Class1.super.method1("value") > } > }); > > Is this possible? > > -- > View this message in context: http://www.nabble.com/Inheritance-questions-tp24996902p24996902.html > Sent from the Dojo mailing list archive at Nabble.com. > > _______________________________________________ > FAQ: http://dojotoolkit.org/support/faq > Book: http://docs.dojocampus.org > Dojo-interest@... > http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest _______________________________________________ FAQ: http://dojotoolkit.org/support/faq Book: http://docs.dojocampus.org Dojo-interest@... http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest |
|
|
Re: Inheritance questionsWhatever you return from one will be passed to the next. If you dont'
return anything, the original arguments will be used. Dustin On Aug 17, 2009, at 9:42 AM, Nathan Toone wrote: > The idea of the constructor is that ALL constructors are called with > the same parameters. You might be able to mess with it a bit using > the preamble - I'm not sure how that would work. > > But the short answer is that changing stuff in the constructor could > lead to unexpected consequences. The main reason being that it is > "guaranteed" that the constructor is called, so many classes take > that as a given - and rely upon that fact. > > You can, however call another method - just using the regular > javascript calls. For example: > > dojo.declare("Class1", null, { > method1: function(a){ > } > }); > > dojo.declare("Class2", Class1, { > method2: function(){ > Class1.prototype.method1.call(this, "My Custom Value"); > } > }); > > the "call" function is a function on a javascript function object > that takes a scope, and then a comma-separated list of parameters to > pass in to the function. That function is called within the given > scope. > > However, in this case, I don't know why you don't just instead do: > this.method1("My Custom Value") > > Since Class2 inherits from Class1, Class2 has that method already. > > -Nathan > > > On Aug 17, 2009, at 2:45 AM, -Robert- wrote: > >> >> I was reading the documentation regarding declaring classes in Dojo >> at >> http://docs.dojocampus.org/dojo/declare >> I have some questions regarding inheritance. >> >> The documentation says that super constructors are called >> automatically. But >> how do you select which arguments are used to call that super >> constructor. >> You might want to call the super constructor with less arguments, >> or pass a >> parameter not given by the arguments in this constructor. >> Example: >> >> dojo.declare("Class1", null, { >> constructor: function(x){ >> this.x=x; >> } >> }); >> >> dojo.declare("Class2", Class1, { >> constructor: function(){ >> // I want to call Class1.super("value") >> } >> }); >> >> >> The other question I have is regarding this.inherited. >> Sometimes I would like to call a super method that is not the same >> as the >> method currently executing. >> And I would like to call it with different parameters than the >> arguments of >> the current method. >> >> Example: >> >> dojo.declare("Class1", null, { >> method1: function(a){ >> } >> }); >> >> dojo.declare("Class2", Class1, { >> method2: function(){ >> // I want to call Class1.super.method1("value") >> } >> }); >> >> Is this possible? >> >> -- >> View this message in context: http://www.nabble.com/Inheritance-questions-tp24996902p24996902.html >> Sent from the Dojo mailing list archive at Nabble.com. >> >> _______________________________________________ >> FAQ: http://dojotoolkit.org/support/faq >> Book: http://docs.dojocampus.org >> Dojo-interest@... >> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest > > _______________________________________________ > FAQ: http://dojotoolkit.org/support/faq > Book: http://docs.dojocampus.org > Dojo-interest@... > http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest _______________________________________________ FAQ: http://dojotoolkit.org/support/faq Book: http://docs.dojocampus.org Dojo-interest@... http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest |
| Free embeddable forum powered by Nabble | Forum Help |