473,770 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with prototype inheritance

Hi,

In my research in the javascript language I have encountered problems
with implementing prototype inheritance while preserving private
methods
functioning properly. Here is an example:

function A(){
var _this = this;
this.a = 10;
var privateFunc = function(){
alert(_this.a)
}
this.publicFunc = function(){
this.a = 20;
privateFunc();
}
}

function B(){
this.publicFunc ();
}

B.prototype = new A();
b = new B();

The expected popup window produced from the following code (alert)
should show 20. Yet it shows 10.

Here we have 2 Class (or rather constructor functions): A, B;

----------------
Class (Constructor) A defines:
----------------
- _this - a private field reflecting "this". It is a local variable
necessary so that "this" can be used in private functions. Otherwise
in such private functions, "this" references to the window object
(i.e. the global javascript object);

- this.a - public field;

- privateFunc - private method of class A;

- this.publicFunc - public method of class A;

Upon instantiation, Class (Constructor) B simply calls the public
method of class A (i.e. publicFunc). publicFunc is referenced from B
through the prototype chain.

The problem, as I see it, is that in this scenario there are two
objects:

B.prototype = new A();
b = new B();

which are instances of class A and class B respectively.
When new A() is called, _this points to this instance (referenced by
B.prototype)
When new B() is called, a seperate object is created (referenced by
b).
When new B() is called, B calls this.publicFunc () where "this" is
actually
the new B object passed to publicFunc() through the prototype chain.
So this new "this", passed from B to publicFunc, is different from
"this" that
was created from the new A() expression. That is why _this (pointing
to "this"
created through new A()) does not coincide with "this" passed from
B(). That's why
this.a is created and attached to object b which is different from
this.a created
through the call of new A(), where _this points.
I see this as a problem to implementing prototype chain inheritance
and use private
methods at the same time (which helps proper encapsulation as required
by OO design).

What do you guys think about this?

Mar 4 '07 #1
2 2098
be*******@gmail .com wrote:
Hi,

In my research in the javascript language I have encountered problems
with implementing prototype inheritance while preserving private
methods
functioning properly. Here is an example:

function A(){
var _this = this;
this.a = 10;
var privateFunc = function(){
alert(_this.a)
}
this.publicFunc = function(){
this.a = 20;
privateFunc();
}
}

function B(){
this.publicFunc ();
}

B.prototype = new A();
b = new B();

The expected popup window produced from the following code (alert)
should show 20. Yet it shows 10.

Here we have 2 Class (or rather constructor functions): A, B;

----------------
Class (Constructor) A defines:
----------------
- _this - a private field reflecting "this". It is a local variable
necessary so that "this" can be used in private functions. Otherwise
in such private functions, "this" references to the window object
(i.e. the global javascript object);

- this.a - public field;

- privateFunc - private method of class A;

- this.publicFunc - public method of class A;

Upon instantiation, Class (Constructor) B simply calls the public
method of class A (i.e. publicFunc). publicFunc is referenced from B
through the prototype chain.

The problem, as I see it, is that in this scenario there are two
objects:

B.prototype = new A();
b = new B();

which are instances of class A and class B respectively.
When new A() is called, _this points to this instance (referenced by
B.prototype)
When new B() is called, a seperate object is created (referenced by
b).
When new B() is called, B calls this.publicFunc () where "this" is
actually
the new B object passed to publicFunc() through the prototype chain.
So this new "this", passed from B to publicFunc, is different from
"this" that
was created from the new A() expression. That is why _this (pointing
to "this"
created through new A()) does not coincide with "this" passed from
B(). That's why
this.a is created and attached to object b which is different from
this.a created
through the call of new A(), where _this points.
I see this as a problem to implementing prototype chain inheritance
and use private
methods at the same time (which helps proper encapsulation as required
by OO design).

What do you guys think about this?
Constructors that introduce privileged methods are, in a sense, final.
The parasitic pattern is effective in this case.

http://javascript.crockford.com/
Mar 4 '07 #2
be*******@gmail .com wrote:
<snip>
I see this as a problem to implementing prototype chain
inheritance and use private methods at the same time
(which helps proper encapsulation as required by OO design).
Is popper encapsulation required by OO design? Making it impossible for
people to do stupid things with objects may save them from doing stupid
things with objects, but being able to does not necessitate that they
will. You are not writing aircraft fly by wire systems or critical
business logic with javascript. A naming convention that tells
programmers using objects defined by others that they should not be
messing with particular sets of properties or methods may well provide
sufficient 'encapsulation' for most practical purposes.
What do you guys think about this?
If you want to use closures to provide objects with private instance
methods and variables you need to create one closure per object. There is
no way round that, and there are 'inheritance' schemes that accommodate
that, but most of the time you don't really need those private members,
you are not creating deep class hierarchies (so maybe don't even need the
inheritance on the objects that really need the private members).

Richard.

Mar 4 '07 #3

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

Similar topics

2
3036
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
4
1771
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now have a function called foo? What if I have 5 different objects, all descended from Ancestor? Do
5
1532
by: tomlong | last post by:
What is the reason why Javascript, unlike other languages, uses prototype instead of classical inheritance? I know you can do classical inheritance with a bit of work, but it seems like the kind of thing that would be nice out of the box, and I don't understand why prototype is preferable. Tom Longson (nym) http://igargoyle.com/
6
1487
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the creation of a class easier to read/understand. Anyway it seems to break the inheritance chain in the following code and I don't know why. window.onload = function() {
6
1885
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance" using the prototype property. I realize there are no classes in JS, that code therefore lives in objects instead of class definitions, and that "inheritance" must be achieved prototypically and not classically. That said, on with the code. Say I...
3
3588
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: Ajax.Base = function() {}; Ajax.Base.prototype = { setOptions: function(options) { <...>
14
2818
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that shows all the properties, weeks available, pricing, etc. for a particular area of Europe. The data varies widely depending on the region; at times there will be 50 properties, and at other times only a half dozen. The cross referencing of...
2
1306
by: VJ | last post by:
I tried to write sample code to get understanding of javascript's prototypal inheritance (along with the variety of function calling choices.. ) During the process, I got myself throughly confused. Can anyone explain following behavior here is my code: <html>
7
1397
by: Jessica | last post by:
Hi, I have a question referring prototypal inheritance in javascript. My example: function A() {}; A.prototype = { count: 10, doit : function () {alert ("doit");} };
0
9618
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
10260
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
9906
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
8933
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
7456
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
6712
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();...
0
5354
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
4007
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
2
3609
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.