You are defining the nest function within the scope of test and it will only be accessible within test. The this.nest function is defined according to the context of your test function which is the namespace variable. This makes that version of nest accessible outside the test function even though you define it within the function body.
Alter your code to this and you will see the different console output;
var namespace = {
test: function() {
//sample 1
function nest(param) {
console.log('a '+ param);
}
nest('sample 1');
//sample 2
this.nest = function(param) {
console.log('b '+ param);
}
this.nest('sample 2');
}
}
namespace.test();
namespace.nest('sample 3');
This will produce the following output;
a sample 1
b sample 2
b sample 3
Some suggested reading on scope and context; Understanding Scope and Context in JavaScript