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 14 1493
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); 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.
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.
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 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
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. 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.' 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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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.
|
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.
|
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...
|
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?
| |
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;
|
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...
|
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?
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |