473,695 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritance question

Hi,

Hopefully the group doesn't mind an(other) inheritance question. Maybe
the prototype inheritance style is starting to become a low dim light
in my brain...probabl y not yet.

----

If I do the following...

var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property. Also, b has all the properties of a.

----

Is it possible to do this more efficiently with a constructor?
Something like...

function B(element){
// what goes here?
}
B.prototype.new _property = function(){aler t("the new property!")};

var a = document.getEle mentById("my_di v");
var b = new B(a);

This way I could efficiently make many b-type objects based on
different a-type objects without having to attach new_property
explicitly to each new b-type object.

----

Thanks,
Peter

Mar 28 '06 #1
14 1492
I keep thinking I should be able to do this but it doesn't work.

function B(element){
this.prototype = element;
}
B.prototype.new _property = function(){aler t("the new property!")};

var a = document.getEle mentById("my_di v");
var b = new B(a);

Mar 28 '06 #2
VK

pe**********@gm ail.com wrote:
Hi,

Hopefully the group doesn't mind an(other) inheritance question. Maybe
the prototype inheritance style is starting to become a low dim light
in my brain...probabl y not yet.

----

If I do the following...

var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property. Also, b has all the properties of a.


Uhmm... Who told you that? a is a reference to a DOM object (presumably
DIV), b is a reference to JavaScript object. They are completely
different species. Did you try say alert(b.tagName ) before posting?

Withing JavaScript program you have to deal with JavaScript objects.
But you can store references onto DOM in these objects.

<html>
<head>
<title>Untitl ed Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function B(dom_obj) {
this.$DOM = dom_obj;
this.new_proper ty = B.new_property;
}
B.new_property = function() {
var msg = 'new property of ' + this.$DOM.id;
window.alert(ms g);
}
function init() {
var a = document.getEle mentById("myDiv ");
var b = new B(a);
b.new_property( );
}
window.onload = init;
</script>
</head>

<body>
<div id="myDiv">&nbs p;</div>
</body>
</html>

btw underscores are not allowed (though tolerated on many UA's) in
element ID, so my_div is changed to myDiv.

Mar 28 '06 #3
On 28/03/2006 10:11, VK wrote:

[snip]
function B(dom_obj) {
this.$DOM = dom_obj;
this.new_proper ty = B.new_property;
}
B.new_property = function() {
var msg = 'new property of ' + this.$DOM.id;
window.alert(ms g);
}
The method, new_property, should be a property of the prototype object
for the B constructor function. It is not, in any way, useful as a
'class' method.

[snip]
btw underscores are not allowed (though tolerated on many UA's) in
element ID, so my_div is changed to myDiv.


ID and NAME tokens must begin with a letter ([A-Za-z]) and may
be followed by any number of letters, digits ([0-9]), hyphens
("-"), underscores ("_"), colons (":"), and periods (".").
^^^^^^^^^^^^^^^ ^^
-- 6.2 SGML basic types, HTML 4.01

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 28 '06 #4

VK wrote:
pe**********@gm ail.com wrote:
If I do the following...

var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property. Also, b has all the properties of a.


Uhmm... Who told you that? a is a reference to a DOM object (presumably
DIV), b is a reference to JavaScript object. They are completely
different species.


Say what?! This sounds extremely strange. I thought that a JavaScript
variable could hold either a primative or a reference to a JavaScript
object. I also thought that document.getEle mentById() returned a
JavaScript object ("instance" of Element "class"). I didn't think there
were other kinds of objects other than JavaScript objects. Perhaps
document.getEle mentById() returns a DOM object wrapped in a JavaScript
object?

Thanks,
Peter

Mar 28 '06 #5
pe**********@gm ail.com wrote:
VK wrote:
pe**********@gm ail.com wrote:
If I do the following...

var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property. Also, b has all the properties of a.


Uhmm... Who told you that? a is a reference to a DOM object (presumably
DIV), b is a reference to JavaScript object. They are completely
different species.


Say what?! This sounds extremely strange. I thought that a JavaScript
variable could hold either a primative or a reference to a JavaScript
object. I also thought that document.getEle mentById() returned a
JavaScript object ("instance" of Element "class"). I didn't think there
were other kinds of objects other than JavaScript objects. Perhaps
document.getEle mentById() returns a DOM object wrapped in a JavaScript
object?


You may find the following year old (March 16, 2005) thread and
subthreads dealing with this issue interesting:
http://groups.google.com/group/comp....f83c684a5f892/

Csaba Gabor from Vienna

Mar 28 '06 #6
On 28/03/2006 19:18, pe**********@gm ail.com wrote:
VK wrote:
pe**********@gm ail.com wrote:
If I do the following...

var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property.
Yes, it does.
Also, b has all the properties of a.

No, it doesn't. Firstly, the prototype property is only significant for
constructor functions. When an object is created using that function and
the new operator, the object referenced by the prototype property of the
constructor is assigned to the internal [[prototype]] property of the
newly created object. It is this internal property that is used when
trying to lookup an object property. See section 8.6.2.1 [[Get]](P) in
ECMA-262. The new object will not have a prototype property.

[snip]
a is a reference to a DOM object (presumably DIV), b is a reference
to JavaScript object. They are completely different species.


Say what?! This sounds extremely strange.


The next thing to note is that there are two broad types of object:
native objects, and host objects.

Native objects are specified by ECMA-262 and include Object, Function,
Array, Math, String, Number, Date, and RegExp. The objects in that list
are also known as built-in objects, as they are available from the
outset of execution. User-defined objects (created using constructor
functions) are also native objects.

Host objects are provided by the environment. All DOM objects are host
objects, as are the 'DOM 0' objects such as location. In IE, ActiveX
objects are also of this category.

Whilst native objects must act as described by ECMA-262, host objects
are at the whim of the implementation in many regards. For instance, in
Gecko browsers, DOM objects have prototype properties that can be
modified to affect relevant DOM nodes, and DOM objects can be the
prototype object of constructor functions. In IE, neither is the case,
but this is permitted.

[snip]

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 28 '06 #7
pe**********@gm ail.com writes:
If I do the following...
var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property. Also, b has all the properties of a.


No. It has two new properties. One called "new_proper ty" and one
called "prototype" . They are both plain properties, with no relevant
difference.

Assigining to a property called "prototype" on an object does not
change that object's actual prototype.
Is it possible to do this more efficiently with a constructor?
You have to use constructors to create a prototype link in Javascript,
so if it can be done, it is done with prototypes.
Something like...

function B(element){
// what goes here?
}
B.prototype.new _property = function(){aler t("the new property!")};

var a = document.getEle mentById("my_di v");
var b = new B(a);
Try
function B() {
this.new_proper ty = function() {alert("the new property");};
}
var a = document.getEle mentById("my_di v");
B.prototype = a;
var b = new B();

This uses B as a constructor, creating a new object (as constructors
do) with "a" as the prototype object of the object.
This way I could efficiently make many b-type objects based on
different a-type objects without having to attach new_property
explicitly to each new b-type object.


If that's the goal, try:

function B(a) {
function Dummy(){};
Dummy.prototype = a;
var b = new Dummy();
b.new_property = function(){aler t("the new property!");
return b;
}

Again, you have to use a constructor to create a new object with
an existing object as its prototype. Javascript does not make that
as easy as I would have liked. The clone function tries to
wrap this up and allow a direct way to create new objects inheriting
from existing ones.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 28 '06 #8
VK

pe**********@gm ail.com wrote:
VK wrote:
pe**********@gm ail.com wrote:
If I do the following...

var a = document.getEle mentById("my_di v");

var b = new Object();
b.prototype = a;
b.new_property = function(){aler t("the new property!");

Now b has the new property. Also, b has all the properties of a.


Uhmm... Who told you that? a is a reference to a DOM object (presumably
DIV), b is a reference to JavaScript object. They are completely
different species.


Say what?! This sounds extremely strange. I thought that a JavaScript
variable could hold either a primative or a reference to a JavaScript
object. I also thought that document.getEle mentById() returned a
JavaScript object ("instance" of Element "class"). I didn't think there
were other kinds of objects other than JavaScript objects.


That's a very common mistake so don't be ashame. In the reality
JavaScript context has nothing in common with DOM tree - but it is able
to communicate with DOM over host object methods.

"East is East, and West is West, and never the twain shall meet" :-)

Actually they shall meet - over DOM interface, but not in such
straightforward way as you originally thought. Did my code snipplet
solve your current task or you need some more help?

Mar 28 '06 #9

VK wrote:
Did my code snipplet solve your current task or you need some more help?


Yes it did help but not really the way I was trying to go with my idea.
I was hoping to get to a place where i could efficiently set up to say
things like "b.style.backgr ound = " where b has inherited from the
element. This would be instead of the second option of b *having* an
element like and saying "b.$DOM.style.b ackground = ". All these posts
have made me realize I should be using the second of these options
since the host objects are such special cases and potentially not
compatible across browsers. Seems like extending a host object through
inheritance is a touchy subject that may not work with the other things
I'm doing. The second option is not limiting me. It just doesn't
emphasize what I was thinking. The second option feels a little more
like a C struct than an object-oriented style of code.

Another thing I've realized is the book I have "JavaScript : The
Definitive Guide 4th edition" by David Flanigan (the rhino book from
O'Reilly) which covers JavaScript 1.5 is no where near definitive when
it comes to the 24-page objects chapter. Also, the book is four years
old now and the way people are coding JavaScript has probably changed a
little over time. Any suggestions for a newer and better book on
JavaScript objects?

Thanks,
Peter

Mar 29 '06 #10

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

Similar topics

1
3745
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2197
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
4
12814
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error below *should* happen, but my question to the community is *why* does it happen? Any answer will be appreciated, but a section and paragraph number from the C++ Standard would be especially appreciated.
8
7828
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
22
23359
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 examples?
45
6342
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
6
2094
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
5
2464
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1834
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to make a list class that will store the objects. My question is how do I go about creating the list class...I am assuming it should be a standalone class that uses an arraylist to store the objects. If I go that route how do I instantiate the...
0
8631
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8984
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
8847
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
8825
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...
1
6491
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
4340
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...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.