require() turns constructor's functions to prototypes?

There is just one behavior of javascript or specifically node.js' require() that I like to understand. I already read this:

Adding new properties to constructor function without .prototype

and this:

The difference between putting functions into an object and prototyping them?

But if you use require() http://docs.nodejitsu.com/articles/getting-started/what-is-require to create new object like:

var Ftp = require("jsftp"),
    ftp = new Ftp({
      host: "ftp.mywebsite.com",
    });

And then let say if you do ftp.getPutSocket() later, it's actually calling the original Ftp.getPutSocket = function() {} in jsftp.js file.

Now how could it be? I thought if you do new Something(), you can only call into Ftp.prototype.getPutSocket. So did require() did some magic here?

UPDATE 1:

So it's nothing to do with require() but basically a function.call(Ftp.prototype) line that the end. But how come this gave me error:

http://jsfiddle.net/qhoc/7j3Vp/

var Test = function() {
    console.log('aa');
}

(function() {
    this.anotherTest = function() {
        console.log('bb');
    }
}).call(Test.prototype);

Error:

Uncaught TypeError: Cannot read property 'prototype' of undefined 

I tried to do the same thing as https://github.com/sergi/jsftp/blob/master/lib/jsftp.js

When getPutSocket is defined (https://github.com/sergi/jsftp/blob/master/lib/jsftp.js#L620), this is actually Ftp.prototype (see https://github.com/sergi/jsftp/blob/master/lib/jsftp.js#L803).

So it doesn't look like Ftp.getPutSocket is defined at all. It is only defined on Ftp.prototype.

Update 1:

This fails because of the order in which javascript evals things. Functions first, then variable assignments.

This however, works:

function Test() {
    console.log('aa');
}

(function() {
    this.anotherTest = function() {
        console.log('bb');
    }
}).call(Test.prototype);

You are getting that error is because of JavaScript's variable hoisting. If you change your code to this:

function Test() {
    console.log('aa');
}

(function() {
    this.anotherTest = function() {
        console.log('bb');
    }
}).call(Test.prototype);

It will just work.

The reason for that is that there are some important differences between declaring a function like

function F() {} // Function declaration

and like

var F = function() {} // Function expression

In the first example, because of it being a declaration, F is parsed and evaluated before any other expressions are. That's why you can do things like:

console.log(myFun());

function myFun() {
  return 'Hi there!';
}

and it will work, even if you are declaring a function after it is used. On the other hand, if you would write it like

console.log(myFun());

var myFun = function() { // expression
  return 'Hi there!';
}

It wouldn't work, because the var expression is being executed after it is actually used, since it is an expression (it is a part of an assignment expression).

In your example, when call evaluates Test.prototype the Test function hasn't been assigned yet, and that's why you get that TypeError.

More good information and insights about this: