V
vwkng1987
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?
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?