Talk is cheap, show me the code
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.
Pages: 1 2
This section gives me most headache. But experience told me that this blog mostly contains personal ramblings related to daily life, open source, and web 2.0. Recently, it turns out to be an idea-box where you can found ideas you can execute in your new startup ;). My ideas and opinions are not bullet proof and never intended to be one way stream. Kindly participate to improve your and my perspective regarding any particular post. Welcome aboard and enjoy your stay
Geek Building The Bridge Part 2 » Silly mistake
December 26th, 2005 at 10:24 am
[...] December 26, 2005 @ 10:21 am · Filed under Dev Hours Remember that javascript problem I was bit***ng about yesterday? I got an enlightment after visiting this page. Actually I was searching for a clone object function. I almost use the code there, but I stumbled upon the “bob and alice question”. If I do bob = alice, what will alice print as message? It’s bob’s message. Holy shit, would have it been a variable reference mess? Ho ho ho, it was indeed. I was doing this in my code: obj.metadata.todo_str = this.mTodo[idx][0]; // adds metadata obj.metadata.todo_priority = this.mTodo[idx][1]; // adds metadata obj.metadata.todo_done = this.mTodo[idx][2]; // adds metadata obj.metadata.just_created = true; obj.metadata.idx = idx; With Metadata object prototyping below: function Metadata() { this.todo_str=‘’; this.todo_priority= 3; this.todo_done= false; this.just_created= true; this.todo_idx= 0 } Node.prototype.metadata = function () { this.todo_str=‘’; this.todo_priority= 3; this.todo_done= false; this.just_created= true; this.todo_idx= 0 } Please, for a while, ignores the redundancy I made there. See, actually I have seen the .. (darn how should I spell “gejala” in english) . let’s called this anomali. So, what I always have in my object properties is the same accross all objects. My hunch said this value come from the last assignment for newly created object. The page I mentioned before really tickles me, waking me up, that this may be a variable by reference hoohaa. After I add this code before the metadata properties assignment obj.metadata = new Metadata; Everything went smooth. Though I still use a dirty way of extending the Node instead of created a derivation from it, I got my problem solved for sure. Inthis meantime, I don;t want to find other workaround, that kinda of shit will distract me from my curent significant goal. As always, till the next code Popularity: 1% [...]