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

Inheritance Chain

Hi everyone
Please look at the code below: (I am picking up JS from Crockfold and
a few other online sources too)

************************************************** *************
function Employee(name){
this.name = name || 'default';
}

function WorkerBee(name, dept){
this.base = Employee; //Not Employee();
this.base(name);
this.dept = dept;
}

var test1 = new WorkerBee('test1', 'Input/Output');

Employee.prototype.speciality = 'none';

((test1.speciality) ? test1.speciality : 'undefined').writeln(); //
returns 'undefined'

WorkerBee.prototype = new Employee;

((test1.speciality) ? test1.speciality : 'undefined').writeln(); //
STILL returns 'undefined'

var test2 = new WorkerBee('test2', 'Computer Science');

test2.speciality.writeln(); //NOW this returns 'none'!

************************************************** *********************************************

My questions are follows:
1) How does the base in WorkerBee works? When I call
var test1 = new WorkerBee('test1', 'Input/Output');
base gets assigned to Employee (as a function) and 'test1' is passed
to Employee as a parameter which return the field "name : 'test1' '"
right?
So If I were to check for the presence of a parameter named Employee
in test1 I would not find anything because what the function of
Employee merely did was pass this statement "name : 'test1' " to the
body of test1. Is this true?
(I did check test1.hasOwnProperty(Employee) and results = false);

2) IF my understanding of prototype is true, then when I called
WorkerBee.prototype = new Employee;
this would like the function WorkerBee's prototype to an Employee
Object, and this Employee Object's prototype field is linked to
another anonymous object with the field 'speciality' = 'none'. If this
were true, how come my second call to find out the speciality of test1
STILL returned null?
Nov 14 '08 #1
6 1436
Also, I discover this while working:

//Following the code above;
function WorkerCee(name, dept){
Employee.call(this, name);
this.dept = dept || '';
}

var Vincent = new WorkerCee('Vincent', 'Morgan-Stanley');
Why does the variable Vincent not have the field speciality EVEN after
employee.prototype.speciality has been declared 'none'??

Nov 14 '08 #2
How does the base in WorkerBee work?

When a new WorkerBee object is created, the following happens,
see 15.3.5.2 of the ECMA-262 Standard:

(a) the script engine makes a new object.
(b) the engine assigns the current value of the 'prototype' property
of the WorkerBee constructor, which at this point is some
'anonymous' object, to the internal [[prototype]] property of the
new object.
(c) the engine calls the WorkerBee function passing the newly
created object as the current value of the 'this' variable.
(d) the WorkerBee function assigns the Employee function as a value
to the 'base' property of the new object.
(e) the WorkerBee function uses the expression 'this.base()'
to call the Employee function with the new object as the
current value of the 'this' variable inside Employee, too.
(f) the Employee function assigns the value of the given 'name'
parameter to the 'name' property of the new object
and returns.
(g) the WorkerBee function finishes by storing the value of
the given 'dept' parameter in the 'dept' property
of the new object.
(h) the WorkerBee function returns the new object to the engine
which assigns it as a reference to the test1 variable.
So If I were to check for the presence of a parameter named Employee
in test1 I would not find anything because what the function of
Employee merely did was pass this statement "name : 'test1' " to the
body of test1. Is this true?
(I did check test1.hasOwnProperty(Employee) and results = false);
Yes!
test1.hasOwnProperty("Employee")==false

But!
test1.hasOwnProperty("base")==true

It would be good practice to include the statement "delete this.base"
in WorkerBee to avoid that situation.
... how come my second call to find out the speciality of test1
STILL returned null?
Because the internal [[prototype]] property of an existing object is
NOT
updated when the value of the 'prototype' property of its constructor
function changes.
When you create test2, that changed prototype is used, however, and
is responsible to give the result 'none'.

Hubert

Nov 14 '08 #3
Ok so let me get this straight...(This is a little hard to explain in
words..)...

1) Whenever you call a constructor, the engine creates an anonymous
object and assigns the value of its parameter to that anonymous
object, but then that object's __proto__ field is assigned the value
of the Constructors' prototype right?

2) When I call another constructor within a constructor, do I also
create ANOTHER ANONYMOUS OBJECT by the method of 1) above?

So in this illustration,
fn_WorkerBee.prototype---->anonymous_object.__proto__ where
anonymous_object.base------>another_anonymous_object created by
Employee's constructor?

3)
"
Because the internal [[prototype]] property of an existing object is
NOT
updated when the value of the 'prototype' property of its constructor
function changes.
When you create test2, that changed prototype is used, however, and
is responsible to give the result 'none'.
"

If i were to indicate the direction of the link with the arrow,
does this mean

fn_workerbee.prototype---->my_Object.__proto__
hence if fn_workerbee.prototype gets any changes my_Object will not be
able to detect since it is in the reverse side of the inheritance
chain

but like what you said, shouldn't the function workerbee have access
to the my_object since fn_workerbee.prototype is directly linked to my
object?

Does this mean that every object created is linked to their
constructor?
Nov 14 '08 #4
disappearedng <di***********@gmail.comwrites:
Ok so let me get this straight...(This is a little hard to explain in
words..)...

1) Whenever you call a constructor, the engine creates an anonymous
object and assigns the value of its parameter to that anonymous
object, but then that object's __proto__ field is assigned the value
of the Constructors' prototype right?
Only if you call the constructor using the <<new Constructor>or <<new
Constructor(...)>statements. Otherwise it works just like a normal
method/function call and no new object is created at all. Also, I'm
assuming you mean it set's <<this>to the newly created object.
2) When I call another constructor within a constructor, do I also
create ANOTHER ANONYMOUS OBJECT by the method of 1) above?
Depends on how you call it: see above.

[ ... lots of stuff snipped ... ]
Does this mean that every object created is linked to their
constructor?
No. Every object has a [[prototype]] that may or may not refer to its
constructor.

I tried to explain most of this some time ago:

http://joost.zeekat.nl/constructors-...confusing.html

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Nov 14 '08 #5
vw*******@gmail.com wrote:
Please look at the code below: (I am picking up JS from Crockfold and
a few other online sources too)
It would appear you picked up the bad examples.
************************************************** *************
function Employee(name){
this.name = name || 'default';
}

function WorkerBee(name, dept){
this.base = Employee; //Not Employee();
this.base(name);
Another, less compatible possibility is

Employee.call(this, name);
this.dept = dept;
}

var test1 = new WorkerBee('test1', 'Input/Output');

Employee.prototype.speciality = 'none';

((test1.speciality) ? test1.speciality : 'undefined').writeln(); //
returns 'undefined'

WorkerBee.prototype = new Employee;
This inheritance pattern, which unfortunately originates from the first
versions of Netscape JavaScript References and has survived in numerous
tutorials to date (even the Wikipedia article), is essentially a wrong
one, meaning that it does not do what it is intended to do.

It does *not* insert the prototype object of `Employee'[1],
`Employee.prototype', in the prototype chain of `Workerbee' objects so that
`Workerbee' objects would inherit from `Employee.prototype':

A: (new WorkerBee) --WorkerBee.prototype --Employee.prototype

Instead, it inserts a new `Employee' object in the prototype chain so that
`WorkerBee' objects inherit from that:

B: (new WorkerBee) --new Employee --Employee.prototype

That little thing makes a big difference when the `Employee' constructor
adds properties to the object or modifies prototype properties, as here,
because the lookup algorithm finds the dynamically added properties of
*the same object* first.

Unless you need behavior B, use the following pattern to achieve A instead:

function inheritFrom(Constructor)
{
function Dummy() {}
Dummy.prototype = Constructor.prototype;
return new Dummy();
}

WorkerBee.prototype = inheritFrom(Employee);

The result is the following prototype chain:

A2: (new WorkerBee) --WorkerBee.prototype --(new Dummy)
--Employee.prototype

Since `Dummy' objects have no properties of their own, they don't interfere
with the property lookup.

NOTE: This isn't exactly news around here.
((test1.speciality) ? test1.speciality : 'undefined').writeln(); //
STILL returns 'undefined'
It's inefficient and harder to maintain to begin with. Consider this instead:

(test1.speciality || 'undefined').writeln();

But since when has a String object (that the primitive string value is being
converted to here) a writeln() method? Maybe you were looking for

document.writeln(test1.speciality || 'undefined');

BTW: Maybe I'm wrong, but I think the word should be "specialty".
var test2 = new WorkerBee('test2', 'Computer Science');

test2.speciality.writeln(); //NOW this returns 'none'!
^^^^^^^^^^^^^^^^^^^^
If this really works chances are you are using some kind of framework that
augments the `String.prototype' object, so all bets are off until you post
the (*stripped-down*) code of that method. By default (per Specification)
String objects do not have or inherit a writeln() method.
************************************************** *********************************************

My questions are follows:
1) How does the base in WorkerBee works? When I call
var test1 = new WorkerBee('test1', 'Input/Output');
base gets assigned to Employee (as a function) and 'test1' is passed
to Employee as a parameter which return the field "name : 'test1' '"
right?
No. In a constructor, `this' refers to the object that is being constructed
with it; not to the constructor itself. So the newly created `Employee'
object is augmented with a `base' property that is called afterwards.
So If I were to check for the presence of a parameter named Employee
in test1 I would not find anything
Correct aside from the use of the term "parameter". You mean a *property*.
because what the function of
Employee merely did was pass this statement "name : 'test1' " to the
body of test1. Is this true?
No. Although this has been explained here ad nauseam before --

<http://jibbering.com/faq/#posting>

--, why not use a debugger to find it out for yourself?

<http://jibbering.com/faq/#debugging>
(I did check test1.hasOwnProperty(Employee) and results = false);
Works as designed. `Employee' is a reference to a Function object.
Object.prototype.hasOwnProperty() expects (of course) as *string value* to
contain the property *name*. If the argument was not of type string, it
would be converted to string. The string representation of `Employee' is
something along "function Employee(...) {\n ...\n}", and there is no
property with *that* name.

But even if you passed "Employee", the method would return `false', for the
reason explained above.
2) IF my understanding of prototype is true, then when I called
WorkerBee.prototype = new Employee;
this would like the function WorkerBee's prototype to an Employee
Object, and this Employee Object's prototype field is linked to
another anonymous object with the field 'speciality' = 'none'.
Roughly speaking that's correct. However, you need to use proper terms: In
ECMAScript implementations, objects have *properties*, not fields. Objects
are *being referred to* (with *references* that are values, and properties
to hold those values), not being linked.
If this were true, how come my second call to find out the speciality
of test1 STILL returned null?
Impossible to say. A `TypeError' exception should have been thrown
("test2.speciality.writeln is not a function"). Either your framework
has interfered, like

String.prototype.writeln = function() {
document.write(this);
};

or you don't post exactly the code that you are using, or you are not
testing nearly as thorough as you think.
HTH

PointedEars
___________
[1] For the sake of brevity I am referring to identifiers as objects, even
though they only represent references to objects of which there can be
more than one.
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 14 '08 #6
disappearedng wrote:
1) Whenever you call a constructor, the engine creates an anonymous
object and assigns the value of its parameter to that anonymous
object, but then that object's __proto__ field is assigned the value
of the Constructors' prototype right?
No. The object is augmented with a `__proto__' *property* with that value
*in (Netscape/Mozilla.org) JavaScript* (so not in Microsoft JScript and
other incompatible implementations).
2) When I call another constructor within a constructor, do I also
create ANOTHER ANONYMOUS OBJECT by the method of 1) above?
No.
So in this illustration,
fn_WorkerBee.prototype---->anonymous_object.__proto__ where
anonymous_object.base------>another_anonymous_object created by
Employee's constructor?
Please restate your request.
3)
"
Because the internal [[prototype]] property of an existing object is
NOT
updated when the value of the 'prototype' property of its constructor
function changes.
When you create test2, that changed prototype is used, however, and
is responsible to give the result 'none'.
"
That's not the reason why. The internal [[prototype]] property does not
need to be updated then without the prototype chain to break, if the latter
has been set up properly in the first place. What properties an object
effectively has (meaning provides as it owns and inherits them) is
determined *on lookup*, not before.
If i were to indicate the direction of the link with the arrow,
There is no link.
does this mean

fn_workerbee.prototype---->my_Object.__proto__
No.
hence if fn_workerbee.prototype gets any changes my_Object will not be
able to detect since it is in the reverse side of the inheritance
chain
Again, only if the prototype chain was set up that way (not properly).
but like what you said, shouldn't the function workerbee have access
to the my_object since fn_workerbee.prototype is directly linked to my
object?
Please restate your request.
Does this mean that every object created is linked to their
constructor?
No.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 14 '08 #7

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

Similar topics

5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
3
by: santosh | last post by:
Hi All , Why does the below code doesn't compile?? class Interface { public: virtual void funA() = 0; virtual void funB() = 0; virtual void funD() = 0; Interface();
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
6
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...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
22
by: Fabio Z | last post by:
Hi all, I have a classic problem: List<Tand List<Xwhere X is a class X : T. Ok, I know the problem: I cannot cast List<Ton List<Xbecause also if X is a T, List<Xis not a List<T>. What I don't...
6
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"...
2
by: beseecher | last post by:
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: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.