From: vwkng1987 on
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?
From: vwkng1987 on
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'??

From: hubert.kauker on
> 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

From: disappearedng on
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?
From: Joost Diepenmaat on
disappearedng <disappearedng(a)gmail.com> writes:

> 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-considered-mildly-confusing.html

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/