473,387 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

prototypical inheritance

Can anyone help here?

I have two classes, Rect and RoundRect.

RoundRect is a subclass of Rect

mstr.chart.Rect = (function(){

function Rect(x, y, w, h) { /* this is the class constructor */
this.x = x;
this.y = y;
this.w = w;
this.h = h;
};

Rect.prototype.toString = function() {
var str = 'x, y: (' + this.x + ',' + this.y + ') w, h : (' +
this.w + ', ' + this.h + ')';
return str;
}

return Rect; })();

mstr.chart.RoundRect = (function() {
function RoundRect(x,y,w,h,arc) {
mstr.chart.Rect.apply(this, arguments);
this.arc = arc;
}

RoundRect.prototype.toString = function() {
return 'testing directly';
}

RoundRect.prototype = new mstr.chart.Rect();

return RoundRect;
})();

-----------------------------------------------------------------------------
When I alert on toString method of RoundRect, it shows me Rect's
toString. What am I doing wrong here?

var myRect = new mstr.chart.RoundRect (0, 0, 2, 2, 0.2);
alert(tooltipRect.toString());

Thanks,
Srini

Aug 17 '07 #1
2 1319
sr*******************@gmail.com wrote:
I have two classes, Rect and RoundRect.
You don't.
RoundRect is a subclass of Rect
It isn't.

http://javascript.crockford.com/javascript.html
mstr.chart.Rect = (function(){

function Rect(x, y, w, h) { /* this is the class constructor */
It isn't. It is the declaration of a method local to the function you are
creating with this lambda FunctionExpression. Properties of the local
Variable Object are usually not available outside their scope (although some
implementations allow that); however, you return a reference to this method,
thereby creating a closure.

Therefore,you can use the returned Function object as constructor of a
prototype object.
this.x = x;
this.y = y;
this.w = w;
this.h = h;
};

Rect.prototype.toString = function() {
var str = 'x, y: (' + this.x + ',' + this.y + ') w, h : (' +
this.w + ', ' + this.h + ')';
return str;
}

return Rect; })();

mstr.chart.RoundRect = (function() {
function RoundRect(x,y,w,h,arc) {
mstr.chart.Rect.apply(this, arguments);
this.arc = arc;
}

RoundRect.prototype.toString = function() {
return 'testing directly';
}

RoundRect.prototype = new mstr.chart.Rect();
First, there is no point in this assignment as it overwrites everything that
was assigned defined before to this object's property. But see below.
return RoundRect;
})();

-----------------------------------------------------------------------------
When I alert on toString method of RoundRect, it shows me Rect's
toString.
[...]
var myRect = new mstr.chart.RoundRect (0, 0, 2, 2, 0.2);
alert(tooltipRect.toString());
(I assume for brevity that `tooltipRect' is a copy-paste error for `myRect',
and that an object reference is the referred object. Also, I omit
`mstr.chart.' as it does not matter here.)

As it should. You assign a new Rect object (below: r) to
RoundRect.prototype. So the prototype chain of RoundRect objects (such as
`myRect' here) looks like

myRect ---r ---Rect.prototype ---Object.prototype ---...

when it should look like

myRect ---RoundRect.prototype ---Rect.prototype ---...

Therefore, myRect.toString refers to Rect.prototype.toString, as `r'
inherits that through *its* prototype chain.
What am I doing wrong here?
IMHO, you do things unnecessarily complicated and thus lose overview.
The above could be rewritten as follows:

function Rect(x, y, w, h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}

Rect.prototype.toString = function()
{
// or one might employ an sprintf(1) equivalent
var str = new Array(
"x, y: (", this.x, ",", this.y, ") w, h : (",
this.w, ", ", this.h, ")"
).join("");

return str;
};

function RoundRect(x, y, w, h, arc)
{
Rect.apply(this, arguments);
this.arc = arc;
}

// Courtesy of Lasse Reichstein Nielsen
// Message-ID: <ve**********@hotpop.com>
function clone(obj)
{
function Dummy() {}
Dummy.prototype = obj;
return new Dummy();
}

RoundRect.prototype = clone(Rect.prototype);
RoundRect.prototype.toString = function()
{
return 'testing directly';
};

var myRect = new RoundRect(0, 0, 2, 2, 0.2);
window.alert(myRect); /* .toString() is implicit with window.alert() */

However, if you need to retain your object model, you have to switch the
assignments to RoundRect.prototype.toString and RoundRect.prototype. Then
you would still have

myRect ---r ---Rect.prototype

as prototype chain, but since r.toString() existed, it would be called
instead of Rect.prototype.toString().

The next step would be to set up a real prototype chain:

RoundRect.prototype = Rect.prototype

However, that would result in

myRect ---RoundRect.prototype === Rect.prototype ---...

and every modification of the RoundRect.prototype object would affect
Rect.prototype as well.

Therefore, the clone() solution I used above, which would result in

myRect ---RoundRect.prototype ---Rect.prototype ---...

as it was probably intended.
HTH

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 17 '07 #2
>
mstr.chart.RoundRect = (function() {
function RoundRect(x,y,w,h,arc) {
mstr.chart.Rect.apply(this, arguments);
this.arc = arc;
}

RoundRect.prototype.toString = function() {
return 'testing directly';
}
// now override the prototype, obliviating the toString method. Oh no!
RoundRect.prototype = new mstr.chart.Rect();

return RoundRect;

})();
Switch them around and it should work:
RoundRect.prototype = new mstr.chart.Rect();
RoundRect.prototype.toString = function() {
return 'testing directly';
}

There are some nice subleties to this approach of extend:
http://developer.yahoo.com/yui/docs/Lang.js.html

The first is that a no-arg constructor is invoked to trigger prototype
inheritance -- not the real constructor, saving you the headache of
unwanted side effects.

Second, they patched around stupid 'ol JScript DontEnum bug.

I mentioned this a few times on YUI team mail (Yahoo internal) and
filed a bug on Y! bugzilla (also internal) and they did it! They
patched for the stupid IE DontEnum bug. Woulda been nice if they'd
made some mention of my name somewhere. It was a pretty clear and
descript bug rep't, too (at least I thought so). I'm my only advocate :
(

I'm not pimping this library, but it is, in my opinion, the best
approach for prototype inheritance). (And I'm a little proud to have
contributed in some slight way to improve the best inheritance
approach).

Garrett
>
-----------------------------------------------------------------------------
When I alert on toString method of RoundRect, it shows me Rect's
toString. What am I doing wrong here?

var myRect = new mstr.chart.RoundRect (0, 0, 2, 2, 0.2);
alert(tooltipRect.toString());

Thanks,
Srini

Aug 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.