The problem is that the init method comes from the Ancestor class. And you are inherit it after the creation of the child instance. For example the code below works.
setTimeout(function() {
test()
}, 200);
function test() {
// Child declaration can go here
Child.prototype = new Ancestor(Child);
var child = new Child();
// If Child is placed here, this.init is undefined
function Child() {
this.init()
}
}
function Ancestor(constructor) {
this.constructor = constructor
}
Ancestor.prototype.init = function init() {
console.log(this)
}
So, just move the Child.prototype = new Ancestor(Child) above the calling of new Child. You don't need to put the Child constructor outside the test function.
In other words, in your example, init method is still not attached to the Child prototype when you call new Child.