473,976 Members | 46,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("<i tems><item>Appl e</item><item>oran ge</
item></items>");
myXML.getItems( );
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFro mString(aString , "text/xml");
return xmlDoc ;
}

xmlDocument.pro totype.getItems (){
var items = xmlDoc.evaluate ("//item",xmlDoc, null,
XPathResult.ANY _TYPE,null);
var thisItem = items.iterateNe xt();
while (thisItem) {
thisItem = items.iterateNe xt();
}
}

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 1760
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("<i tems><item>Appl e</item><item>oran ge</
item></items>");
myXML.getItems( );
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFro mString(aString , "text/xml");
return xmlDoc ;

}

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

xmlDocument.pro totype.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("<i tems><item>Appl e</item><item>oran ge</
item></items>");
myXML.getItems( );
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFro mString(aString , "text/xml");
return xmlDoc ;
}
xmlDocument.pro totype.getItems (){

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

xmlDocument.pro totype.getItems = function(){ ... }

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

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

Here's my code again :

function initXML() {
var myXML = new xmlDocument("<i tems><item>Appl e</
item><item>oran ge</
item></items>");
myXML.getItems( );
}
function xmlDocument( aString ){
var parser = new DOMParser();
var xmlDoc = parser.parseFro mString(aString , "text/xml");
return xmlDoc ;

}

xmlDocument.pro totype.getItems = function(){
var items = xmlDoc.evaluate ("//item",xmlDoc, null,
XPathResult.ANY _TYPE,null);
var thisItem = items.iterateNe xt();
while (thisItem) {
thisItem = items.iterateNe xt();
}
}
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.pro totype.getItems = function(){ ... } ;
Must have accidentally deleted the 'function' keyword while copy
pasting, sorry !

Here's my code again :

function initXML() {
var myXML = new xmlDocument("<i tems><item>Appl e</
item><item>oran ge</
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.parseFro mString(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.parseFro mString(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.pro totype.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.eva luate("//item", this.xmlDoc, null,

XPathResult.ANY _TYPE,null);
var thisItem = items.iterateNe xt();
while (thisItem) {
thisItem = items.iterateNe xt();
}
}

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>xm lDoc</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.parseFro mString(aString , "text/xml");
}

xmlDocument.pro totype.getItems = function(){
var items = this.xmlDoc.eva luate("//item", this.xmlDoc, null,
XPathResult.ANY _TYPE, null);
var thisItem = items.iterateNe xt();
while (thisItem) {

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

thisItem = items.iterateNe xt();
}
}

initXML('<items ><item>Apple</item><item>oran ge</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
1660
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; this.getName = function() { return this.name;
5
2010
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() { var objectName = "ParentObject";
21
6079
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 good chance I will write this as I need new widgets. I haven't found anything like this and I'm surprised/disapointed this doesn't already exist. My library prototype works nicely. I think parts of these ideas are not commonly used for JavaScript...
13
2154
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 parts of the code, but as far as I can see, the way objects work in javascript is quite awkward. I had it working the way they suggest in the book, and it was going OK until I wanted to call one object method from another - I couldn't find a...
2
308
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 I need to to fit my site and then put them in. I have dabbed alittle bit into computer programming some years ago. Trying to teach myself and found it overwhelming. So I dont know how the process is going to be in learning javascript. I'm not...
18
1949
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 requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
1
25731
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
2
2516
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 seem very offensive to him. For over a year, Crockford has proposed an alternate way of using prototypes like this function object(o) { function F() {} F.prototype = o; return new F();
6
1575
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. (SEO)?
6
3220
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) http://stanleyxu2005.blogspot.com
0
10172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11408
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11576
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10084
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6416
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3762
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.