Modules and Javascript Inheritance

91 views
Skip to first unread message

satori

unread,
Feb 10, 2010, 9:11:32 PM2/10/10
to nodejs
Hello. I am having trouble understanding how javascript inheritence
and nodejs modules work.
The code below works ok for me if it is one module, but not when in
sperate modules.
I do not understand why. Any help would be appreciated.


######## main.js ########
var assert = require("assert");
var BaseObject = require("./base").BaseObject;
var ObjectA = require("./a").ObjectA;
var objA = new ObjectA();
assert.ok(objA instanceof BaseObject); // FAILS!

######## base.js ########
function BaseObject() {};
exports.BaseObject = BaseObject;

######## a.js ########
var BaseObject = require("./base").BaseObject;
function ObjectA() {
BaseObject.call(this);
};
process.inherits(ObjectA, BaseObject);
exports.ObjectA = ObjectA;


christkv

unread,
Feb 11, 2010, 5:45:35 AM2/11/10
to nodejs
Hi Satori

I had the same problem and it happened when node 0.1.28 was released
(I think it has something to do with the new way the modules are
loaded). I solved it by adding a className variable in my objects and
then using that to check the type of object instead of instanceof

exports.BaseObject = function() {
this.className = "BaseObject";
}

assert(objA.className == "BaseObject")

Anyone know any other way ?

Christian

Felix Geisendörfer

unread,
Feb 11, 2010, 6:14:09 AM2/11/10
to nodejs
Sorry about that. I got this wrong when I simplified the module
system, but this patch should fix it:

http://github.com/felixge/node/commit/4718208a138a68a6b0047d6734130981b827e9ee

Let me know what you think ryan,
--fg

satori

unread,
Feb 11, 2010, 7:32:17 AM2/11/10
to nodejs
Thanks! I thought was doing something incorrect.

On Feb 11, 10:14 pm, Felix Geisendörfer <fe...@debuggable.com> wrote:
> Sorry about that. I got this wrong when I simplified the module
> system, but this patch should fix it:
>

> http://github.com/felixge/node/commit/4718208a138a68a6b0047d673413098...

Ryan Dahl

unread,
Feb 11, 2010, 2:28:32 PM2/11/10
to nod...@googlegroups.com
On Thu, Feb 11, 2010 at 4:32 AM, satori <blake....@gmail.com> wrote:
> Thanks! I thought was doing something incorrect.
>
> On Feb 11, 10:14 pm, Felix Geisendörfer <fe...@debuggable.com> wrote:
>> Sorry about that. I got this wrong when I simplified the module
>> system, but this patch should fix it:
>>
>> http://github.com/felixge/node/commit/4718208a138a68a6b0047d673413098...
>>
>> Let me know what you think ryan,

Thanks Felix, landed in b02b54e.

Reply all
Reply to author
Forward
0 new messages