Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > inheritance problem

Reply
Thread Tools

inheritance problem

 
 
0m4r
Guest
Posts: n/a
 
      08-16-2007
Hello,
I'm quite new with Javascript and so actually I'm trying to learn
something about it.
I want to "extend" the Image DOM element, so I wrote this Javascript
code:

=== MyImg.js ===
function MyImg(imgsrc){
this.src = imgsrc;
}
MyImg.prototype = new Image();
=== /MyImg.js ===

Then I put this code int an HTML page:

=== index.html ==
[...some HTML header stuff...]
<script language=..... >
function load(){

var container = document.getElementById("test1");
var container1 = document.getElementById("test2");

var gImg = new MyImg("Image1.jpg");
var gImg1 = new MyImg("Image2.jpg");

container.appendChild(gImg);
container.appendChild(gImg1);

container1.innerHTML = gImg.src + "<br>" + gImg1.src;
</script>
</head>
<body onload="load">
<div id="test1"/>
<div id="test2"/>
</body>
</html>
=== /index.html ===

and here is the result I have back:

=== generated HTML code ===
<div id="test1">
<img src="Image2.jpg"/>
</div>
<div id="test2">
Image2.jpg<br/>Image2.jpg
</div>
=== /generated HTML code ===

Instead of what I expect:

=== expected HTML code ===
<div id="test1">
<img src="Image1.jpg"/>
<img src="Image2.jpg"/>
</div>
<div id="test2">
Image1.jpg<br/>Image2.jpg
</div>
=== /expected HTML code ===


Where I'm wrong? Could anybody help me?

Thanks a lot,
Omar

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 11:02 pm, 0m4r <omar.adob...@gmail.com> wrote:
> Hello,
> I'm quite new with Javascript and so actually I'm trying to learn
> something about it.
> I want to "extend" the Image DOM element, so I wrote this Javascript
> code:
>
> === MyImg.js ===
> function MyImg(imgsrc){
> this.src = imgsrc;}
>
> MyImg.prototype = new Image();
> === /MyImg.js ===
>
> Then I put this code int an HTML page:
>
> === index.html ==
> [...some HTML header stuff...]
> <script language=..... >


The language attribute is deprecated, type is required.


> function load(){
>
> var container = document.getElementById("test1");
> var container1 = document.getElementById("test2");
>
> var gImg = new MyImg("Image1.jpg");
> var gImg1 = new MyImg("Image2.jpg");
>
> container.appendChild(gImg);
> container.appendChild(gImg1);


appendChild expects its argument to be a HTML element that implements
the Node interface. For some browsers, your use of new Image() on the
prototype chain fits the bill, in others, not. It is a risky strategy
that is bound to fail a good percentage of the time.


> container1.innerHTML = gImg.src + "<br>" + gImg1.src;
> </script>
> </head>
> <body onload="load">


In order to execute the function referenced by 'load', you need to add
the call operator:

<body onload="load()">


> <div id="test1"/>


That is invalid HTML. For div elements, the closing tag is required.


> <div id="test2"/>
> </body>
> </html>
> === /index.html ===
>
> and here is the result I have back:


In some browsers you will get a syntax error due to the previously
mentioned issue with appendChild.


> === generated HTML code ===
> <div id="test1">
> <img src="Image2.jpg"/>


I'll bet there isn't a browser out there that returns that as HTML.
Firefox returns:

<div id="test1">
<div id="test2">file:///Users/rogreen/Desktop/Image2.jpg<br>file:///
Users/rob/Desktop/Image2.jpg</div><img src="Image2.jpg"></div>

> </div>
> <div id="test2">
> Image2.jpg<br/>Image2.jpg
> </div>

[...]
>
> Where I'm wrong? Could anybody help me?


Use valid HTML. If you want to build DOM Image elements, then do
that.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>foo</title>
<script type="text/javascript">

function load(){

var container = document.getElementById("test1");
var container1 = document.getElementById("test2");

var gImg = new Image();
gImg.src = "Image1.jpg";
var gImg1 = new Image();
gImg1.src = "Image2.jpg";

container.appendChild(gImg);
container.appendChild(gImg1);
container1.innerHTML = gImg.src + "<br>" + gImg1.src;
}
</script>
</head>
<body onload="load()">
<div id="test1"></div>
<div id="test2"></div>
</body>

 
Reply With Quote
 
 
 
 
Marcos Toledo
Guest
Posts: n/a
 
      08-16-2007
Hi Omar, Rob,

This question actually got me thinking a lot. Although I agree with
Rob in the details about your HTML (div closing, languague, etc), and
all the discouraging about extending the "Image" DOM Object, I think
the question has bigger merits, even if theoretically, which got me
thinking as well.

Your actual result contain only a single element inside the test div,
instead of the expected 2. And it is, apparently, only the second
element, but I noticed something:

When you define "Image" as your prototype, by the time you instantiate
the first Image, all of the attributes your object has are the
attributes found in the Image prototype. This means that, by the time
you are assining this.src the value in the parameter, this is an
actual image.

When you create your second instance, with "Image2.jpg", due to the
prototype chain, you also have all of the attributes found in the
Image prototype assined the this. Though, the weird part is:

*The src attribute found when creating the second image, is
"Image1.jpg"*

This means that, internally, the 1st object and the 2nd object created
through your constructors are actually the same. The src in the 1st is
overriden with the 2nd, and the appendChild fails, because the object
has already been appended.

This wouldn't happen if you mimicked all of the DOM Image object's
attributes and assigned it to another non-dom object, say, Image2
{src:''};, and added that new object to the prototype instead of
Image.

This has led me to the conclusion that the prototype chain works in a
different way when you have DOM Objects in it, than when you have
actual objects.

Am I missing something?

Anyway, good question.

[]s
Toledo


 
Reply With Quote
 
0m4r
Guest
Posts: n/a
 
      08-17-2007
> > function load(){
>
> > var container = document.getElementById("test1");
> > var container1 = document.getElementById("test2");

>
> > var gImg = new MyImg("Image1.jpg");
> > var gImg1 = new MyImg("Image2.jpg");

>
> > container.appendChild(gImg);
> > container.appendChild(gImg1);

>
> appendChild expects its argument to be a HTML element that implements
> the Node interface. For some browsers, your use of new Image() on the
> prototype chain fits the bill, in others, not. It is a risky strategy
> that is bound to fail a good percentage of the time.


Mmmmh... So which would be a good solution to prevent this?


> Use valid HTML. If you want to build DOM Image elements, then do
> that.
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <head>
> <title>foo</title>
> <script type="text/javascript">
>
> function load(){
>
> var container = document.getElementById("test1");
> var container1 = document.getElementById("test2");
>
> var gImg = new Image();
> gImg.src = "Image1.jpg";
> var gImg1 = new Image();
> gImg1.src = "Image2.jpg";
>
> container.appendChild(gImg);
> container.appendChild(gImg1);
> container1.innerHTML = gImg.src + "<br>" + gImg1.src;
> }
> </script>
> </head>
> <body onload="load()">
> <div id="test1"></div>
> <div id="test2"></div>
> </body>


I fix the <div> issue, but It still doesn't work... I don't want to
use the Image element but a sort of "extension" of the Image DOM
element....

Thanks for your answer!

 
Reply With Quote
 
0m4r
Guest
Posts: n/a
 
      08-17-2007
On Aug 16, 4:50 pm, Marcos Toledo <mtol...@gmail.com> wrote:
> Hi Omar, Rob,
>
> This question actually got me thinking a lot. Although I agree with
> Rob in the details about your HTML (div closing, languague, etc), and
> all the discouraging about extending the "Image" DOM Object, I think
> the question has bigger merits, even if theoretically, which got me
> thinking as well.
>
> Your actual result contain only a single element inside the test div,
> instead of the expected 2. And it is, apparently, only the second
> element, but I noticed something:
>
> When you define "Image" as your prototype, by the time you instantiate
> the first Image, all of the attributes your object has are the
> attributes found in the Image prototype. This means that, by the time
> you are assining this.src the value in the parameter, this is an
> actual image.
>
> When you create your second instance, with "Image2.jpg", due to the
> prototype chain, you also have all of the attributes found in the
> Image prototype assined the this. Though, the weird part is:
>
> *The src attribute found when creating the second image, is
> "Image1.jpg"*
>
> This means that, internally, the 1st object and the 2nd object created
> through your constructors are actually the same. The src in the 1st is
> overriden with the 2nd, and the appendChild fails, because the object
> has already been appended.


This is what I was supposing too... You are confirming my thoughts!

> This wouldn't happen if you mimicked all of the DOM Image object's
> attributes and assigned it to another non-dom object, say, Image2
> {src:''};, and added that new object to the prototype instead of
> Image.


How can I do this? I can't figure out how to reproduce the behavior
you are talcking about.
could you pleas wrote me a simple snippet?

anyway, thanks for your answer!

Omar


 
Reply With Quote
 
Apekatthjerne
Guest
Posts: n/a
 
      08-17-2007
On Aug 17, 11:49 am, 0m4r <omar.adob...@gmail.com> wrote:
> On Aug 16, 4:50 pm, Marcos Toledo <mtol...@gmail.com> wrote:
>
>
>
> > Hi Omar, Rob,

>
> > This question actually got me thinking a lot. Although I agree with
> > Rob in the details about your HTML (div closing, languague, etc), and
> > all the discouraging about extending the "Image" DOM Object, I think
> > the question has bigger merits, even if theoretically, which got me
> > thinking as well.

>
> > Your actual result contain only a single element inside the test div,
> > instead of the expected 2. And it is, apparently, only the second
> > element, but I noticed something:

>
> > When you define "Image" as your prototype, by the time you instantiate
> > the first Image, all of the attributes your object has are the
> > attributes found in the Image prototype. This means that, by the time
> > you are assining this.src the value in the parameter, this is an
> > actual image.

>
> > When you create your second instance, with "Image2.jpg", due to the
> > prototype chain, you also have all of the attributes found in the
> > Image prototype assined the this. Though, the weird part is:

>
> > *The src attribute found when creating the second image, is
> > "Image1.jpg"*

>
> > This means that, internally, the 1st object and the 2nd object created
> > through your constructors are actually the same. The src in the 1st is
> > overriden with the 2nd, and the appendChild fails, because the object
> > has already been appended.

>
> This is what I was supposing too... You are confirming my thoughts!
>
> > This wouldn't happen if you mimicked all of the DOM Image object's
> > attributes and assigned it to another non-dom object, say, Image2
> > {src:''};, and added that new object to the prototype instead of
> > Image.

>
> How can I do this? I can't figure out how to reproduce the behavior
> you are talcking about.
> could you pleas wrote me a simple snippet?
>
> anyway, thanks for your answer!
>
> Omar



I just use this function:

//START
function downloadImage()
{
for(var i = 0, argumentsLength = arguments.length, newImages = []; i
< argumentsLength; i++)
{
newImages[i] = new Image;
newImages[i].src = arguments[i];
}
}
//END

So you can call as many as you wish. For example:
//START
downloadImage('logo.gif'); // just one
downloadImage('logo.gif', 'example.jpg', 'footer.png'); // let's have
a few
//END

Apekatthjerne

 
Reply With Quote
 
0m4r
Guest
Posts: n/a
 
      10-01-2007
On Aug 17, 1:29 pm, Apekatthjerne <sidney.robe...@gmail.com> wrote:

> I just use this function:
>
> //START
> function downloadImage()
> {
> for(var i = 0, argumentsLength = arguments.length, newImages = []; i
> < argumentsLength; i++)
> {
> newImages[i] = new Image;
> newImages[i].src = arguments[i];
> }}
>
> //END
>
> So you can call as many as you wish. For example:
> //START
> downloadImage('logo.gif'); // just one
> downloadImage('logo.gif', 'example.jpg', 'footer.png'); // let's have
> a few
> //END
>
> Apekatthjerne





Your solution is not the one I was, and I'm actually, looking for...
I need to "extend" a standard DOM object and, as it looks, is ont
possible!

 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      10-01-2007
On Aug 16, 6:02 am, 0m4r <omar.adob...@gmail.com> wrote:
> Hello,
> I'm quite new with Javascript and so actually I'm trying to learn
> something about it.


<URL: http://www.jibbering.com/faq/>
<URL: http://www.jibbering.com/faq/#FAQ3_1>
<URL: http://groups.google.com/group/comp.lang.javascript/msg/04a7a303c2e67280>

Peter



 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      10-02-2007
On Aug 17, 6:45 am, 0m4r <omar.adob...@gmail.com> wrote:
> > > function load(){

>
> > > var container = document.getElementById("test1");
> > > var container1 = document.getElementById("test2");

>
> > > var gImg = new MyImg("Image1.jpg");
> > > var gImg1 = new MyImg("Image2.jpg");

>
> > > container.appendChild(gImg);
> > > container.appendChild(gImg1);

>
> > appendChild expects its argument to be a HTML element that implements
> > the Node interface. For some browsers, your use of new Image() on the
> > prototype chain fits the bill, in others, not. It is a risky strategy
> > that is bound to fail a good percentage of the time.

>
> Mmmmh... So which would be a good solution to prevent this?
>
>
>
>
>
> > Use valid HTML. If you want to build DOM Image elements, then do
> > that.

>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
> > "http://www.w3.org/TR/html4/strict.dtd">
> > <head>
> > <title>foo</title>
> > <script type="text/javascript">

>
> > function load(){

>
> > var container = document.getElementById("test1");
> > var container1 = document.getElementById("test2");

>
> > var gImg = new Image();
> > gImg.src = "Image1.jpg";
> > var gImg1 = new Image();
> > gImg1.src = "Image2.jpg";

>
> > container.appendChild(gImg);
> > container.appendChild(gImg1);
> > container1.innerHTML = gImg.src + "<br>" + gImg1.src;
> > }
> > </script>
> > </head>
> > <body onload="load()">
> > <div id="test1"></div>
> > <div id="test2"></div>
> > </body>

>
> I fix the <div> issue, but It still doesn't work... I don't want to
> use the Image element but a sort of "extension" of the Image DOM
> element....
>


An Image object is not necessarily an image element. Forget the Image
constructor and use createElement to create image elements. Then set
their src properties. No "extensions" necessary.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57