Your problem is way simpler than this (though a little tough to explain). 
The problem in your example is that you are creating a dynamic object based 
on the AsLinker constructor before you have finished setting up the 
constructor. See my comments on your example.

// Pull in your dependencies
var    util = require("util"),
        AsLinker = require("./AsLinker");

// You're creating an object here at runtime. It's based on 
// the prototype of SomeLinker. But where did SomeLinker come from?
module.exports = new SomeLinker();

// This function definition gets hoisted to the top of this execution scope 
because it's a function
// statement, so SomeLinker is in scope when the module.exports line runs. 
This js quirk is the
// ONLY reason the line above works.
function SomeLinker(){ 

    AsLinker.call(this);
}

// This is where your problem is, this line dynamically modifies the 
SomeLinker constructor
// But you've already used the SomeLinker above before this line executes
util.inherits(SomeLinker, AsLinker);

// This still works because any objects created before this still chain to 
this prototype.
// But it'll be the wrong prototype object.
SomeLinker.prototype.link = function(){ // override the parent method };

I think you understand the problem. But changing util.inherits to not 
dynamically override the prototype isn't the best way to tackle it. Instead 
you should make sure that your constructor is completely set up before 
using it, because the prototype and some other elements might dynamically 
change. That's how js works. In classical inheritance terms, it would be 
like stopping in the middle of delcaring a new class Foo and calling new 
Foo(). You wouldn't expect that to work, and in fact it's impossible 
because you can't execute things in the middle of a class declaration. But 
this is javascript, which means we don't have syntax that makes sure 
setting up a new object type is atomic (yet). So since setting up a new 
type is dynamic, that means you *can* execute things in the middle of or 
even before a "class" declaration in js. But you shouldn't. Problems like 
this are what annoy a lot of people about js.

But in your case, the fix is really simple. You have a couple of options.

1) Put your module.exports call at the bottom of the file after all the 
setup has run.

2) Since it looks like you want a singleton object, just use a mixin.

var linkerOverrides =  { link: function() { /* override method */ } };
module.exports = mixin(new AsLinker(), linkerOverrides);

Hope this helps
:Marco

PS - mixin function not provided


On Monday, March 4, 2013 9:24:07 PM UTC-8, [email protected] wrote:
>
>   Post reply
> [image: More message actions]
>  1:19 PM (1 minute ago) 
>   Hi...wondering if this is possible for the method.
>
> I know util.inherits() might be implemented in this way.
>
> function inherits(constructor, super){
>
>     constructor.prototype = Object.create(super.prototype);
>     constructor.prototype.constructor = constructor;
>     constructor.super_ = super;
> }
>
> I am wondering if it could be changed to the following way:
>
> function inherits(constructor, super){
>
>     constructor.prototype.__proto__ = super.prototype;
>     constructor.super_ = super;
> }
>
> The main difference is the second implementation won't change the 
> constructor.prototype, and the benefit is we can use module.exports more 
> flexible, e.g.
>
> var    util = require("util"),
>         AsLinker = require("./AsLinker");
>
> module.exports = new SomeLinker(); // put the module.exports line in the 
> beginning of the script
>
> function SomeLinker(){ 
>
>     AsLinker.call(this);
> }
>
> util.inherits(SomeLinker, AsLinker);
>
> SomeLinker.prototype.link = function(){ // override the parent method };
>
> Since the SomeLinker.prototype is the original one, we can still update 
> the SomeLinker.prototype after module.exports = new SomeLinker();
> In the current implementation of util.inherits(), we must move the 
> module.exports = new SomeLinker() to the bottom of the script after the 
> method is added to the SomeLinker.prototype.
> Or the instance of SomeLinker will have no chance to get the new prototype.
>
> Just a thought to improve the convenience of Node.js coding, apologized if 
> I am wrong, since I know it's been locked status. But the interface is the 
> same, maybe it could be changed :)
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to