473,322 Members | 1,781 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,322 software developers and data experts.

Javascript inheritance

I'm just reading up on OO Javascript but I just can't seem it to work

Here's what my code looks like :

function initXML() {
var myXML = new xmlDocument("<items><item>Apple</item><item>orange</
item></items>");
myXML.getItems();
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(aString, "text/xml");
return xmlDoc ;
}

xmlDocument.prototype.getItems(){
var items = xmlDoc.evaluate("//item",xmlDoc, null,
XPathResult.ANY_TYPE,null);
var thisItem = items.iterateNext();
while (thisItem) {
thisItem = items.iterateNext();
}
}

However, when I try it out in FireFox 2 and call initXML I get :
Error: myXML.getItems is not a function

The above given way of inheritance works fine for user-defined objects
but gets stuck whenever the constructor returns a system-defined
object..

Any idea what I'm doing wrong ?

Feb 21 '07 #1
4 1736
On Feb 21, 10:21 am, "NaReN" <kingc...@gmail.comwrote:
I'm just reading up on OO Javascript but I just can't seem it to work

Here's what my code looks like :

function initXML() {
var myXML = new xmlDocument("<items><item>Apple</item><item>orange</
item></items>");
myXML.getItems();
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(aString, "text/xml");
return xmlDoc ;

}

xmlDocument.prototype.getItems(){
That will (attempt to) call getItems(), I think you want to assign a
function reference to it:

xmlDocument.prototype.getItems = function(){ ... }
You shouldn't assume that you can modify host objects or their
prototypes - test thoroughly.
--
Rob

Feb 21 '07 #2
On Feb 20, 9:09 pm, "RobG" <r...@iinet.net.auwrote:
On Feb 21, 10:21 am, "NaReN" <kingc...@gmail.comwrote:
I'm just reading up on OO Javascript but I just can't seem it to work
Here's what my code looks like :
function initXML() {
var myXML = new xmlDocument("<items><item>Apple</item><item>orange</
item></items>");
myXML.getItems();
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(aString, "text/xml");
return xmlDoc ;
}
xmlDocument.prototype.getItems(){

That will (attempt to) call getItems(), I think you want to assign a
function reference to it:

xmlDocument.prototype.getItems = function(){ ... }

You shouldn't assume that you can modify host objects or their
prototypes - test thoroughly.

--
Rob
Oops, I did use xmlDocument.prototype.getItems = function(){ ... } ;
Must have accidentally deleted the 'function' keyword while copy
pasting, sorry !

Here's my code again :

function initXML() {
var myXML = new xmlDocument("<items><item>Apple</
item><item>orange</
item></items>");
myXML.getItems();
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(aString, "text/xml");
return xmlDoc ;

}

xmlDocument.prototype.getItems = function(){
var items = xmlDoc.evaluate("//item",xmlDoc, null,
XPathResult.ANY_TYPE,null);
var thisItem = items.iterateNext();
while (thisItem) {
thisItem = items.iterateNext();
}
}
Thanks Rob ! Now, I wanted to extend default system objects , how
would I go about doing it ?

Feb 21 '07 #3
On Feb 21, 12:26 pm, "NaReN" <kingc...@gmail.comwrote:
[...]
Oops, I did use xmlDocument.prototype.getItems = function(){ ... } ;
Must have accidentally deleted the 'function' keyword while copy
pasting, sorry !

Here's my code again :

function initXML() {
var myXML = new xmlDocument("<items><item>Apple</
item><item>orange</
item></items>");
Manually wrap code at about 70 characters, auto-wrapping will nearly
always introduce errors. Use 2 spaces for indents too.

myXML.getItems();
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(aString, "text/xml");
When you call this function as a constructor, it will return the newly
created object. So that you can conveniently set properties of the
new object, it is set as the value of the function's this keyword.
You want to return an object that has an "xmlDoc" property, so:

this.xmlDoc = parser.parseFromString(aString, "text/xml");

return xmlDoc ;
And since the new object is returned when called as a constructor,
there is no need for a return statement - just remove it.
>
}

xmlDocument.prototype.getItems = function(){
var items = xmlDoc.evaluate("//item",xmlDoc, null,
The variable 'xmlDoc' is a property of the object calling this
function along the prototype chain. To get that property, use the
this keyword:

var items = this.xmlDoc.evaluate("//item", this.xmlDoc, null,

XPathResult.ANY_TYPE,null);
var thisItem = items.iterateNext();
while (thisItem) {
thisItem = items.iterateNext();
}
}

Thanks Rob ! Now, I wanted to extend default system objects , how
would I go about doing it ?
You might be able to find documentation that tells you whether you can
or not, but you may find it difficult. If you can't find any, try it
and see. Then ask here (or wherever) to see if someone else knows.

Here is a working version:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>xmlDoc</title>
<style type="text/css"></style>
<script type="text/javascript">
//<![CDATA[
function initXML(s) {
var myXML = new xmlDocument(s);
myXML.getItems();
}

function xmlDocument( aString ){
var parser = new DOMParser();
this.xmlDoc = parser.parseFromString(aString, "text/xml");
}

xmlDocument.prototype.getItems = function(){
var items = this.xmlDoc.evaluate("//item", this.xmlDoc, null,
XPathResult.ANY_TYPE, null);
var thisItem = items.iterateNext();
while (thisItem) {

// Debug...
alert(thisItem.textContent);

thisItem = items.iterateNext();
}
}

initXML('<items><item>Apple</item><item>orange</item></items>');
//]]>
</script>
</head>
<body>
<div></div>
</body></html>
--
Rob

Feb 21 '07 #4
Thanks a lot Rob, that helped a lot.

Feb 21 '07 #5

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

Similar topics

11
by: Vincent van Beveren | last post by:
Hi everyone, I have the following code using inheritence The important part is the function getName(). The result should alert 'John Doe': function First(name) { this.name = name; ...
5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
21
by: petermichaux | last post by:
Hi, I've been asking questions about library design over the last week and would like to get feedback on my overall idea for a JavaScript GUI library. I need a nice GUI library so there is a...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
2
by: rich | last post by:
I'd like to improve my webdesign knowledge and learn how to write Javascripts. I have built my own website. I have javascripts on my site that I havent written. I download them and edit them where...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Peter Michaux | last post by:
Douglas Crockford doesn't seem to like JavaScript's built-in syntax for building new objects based on a prototype object. The constructor function, its prototype property and the "new" keyword all...
6
by: John | last post by:
Hello, I was thinking of trying out JavaScript and learn it on my own. I had a few questions though. Is it still popular with businesses today? Is JavaScript bad for search engine opt....
6
by: Xu, Qian | last post by:
Hello All, is there any handy tool to generate class diagrams for javascript? I have tried JS/UML, but it generates always an empty diagram. -- Xu, Qian (stanleyxu)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.