Just to sum everything before I go with the main “meal”, my office gateway went puff this noon. What left is a bitchy feeling for unable to browse anything but Google (I have google ip handy here)


So, what’s the fuss with javascript? Well, I just found this noon that code sequence is, indeed, important issue in javascript, especially when dealing with object prototyping.

Given this example:

function Metadata() {
this.foo = 'metadata';
}

function Employee () {
this.name = "";
this.dept = "general";
}

function Manager () {
this.reports = [];
}
Manager.prototype = new Employee;

function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;

function SalesPerson () {
this.dept = “sales”;
this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;

function Engineer () {
this.dept = “engineering”;
this.machine = “”;
}
Engineer.prototype = new WorkerBee;

// notice this prototyping below
Employee.prototype = new Metadata;

// instances

var toni = new Engineer;
toni.name = ‘Sutoni’;

var foo = new SalesPerson;
foo.foo = ‘Sales Person’;
foo.name = ‘Sufoobar’;

document.write(’toni.foo: ‘ + toni.foo + ‘
‘);
document.write(’toni.name: ‘ + toni.name+ ‘
‘);
document.write(’foo.foo: ‘ + foo.foo + ‘
‘);
document.write(’foo.name: ‘ + foo.name + ‘
‘);

Guess what will happen as output?

toni.foo: metadata

rite? Well, this is what I got on my Firefox 1.5

toni.foo: undefined
toni.name: Sutoni
foo.foo: Sales Person
foo.name: Sufoobar




WTF? How come toni.foo is undefined? well, maybe because it’s a interpreted language nature. As we define the inheritance (as other language suppose to call prototyping in Javascript. Note: prototyping != inheritance) in the last part, the former code were not aware of it. Thus adding the inheritance in the last minute was futile. It’s just simply not conencted to the previous class (prototype) declaration.


Let’s fix it then.

Sphere: Related Content

Pages: 1 2