Can JavaScript constructor return function and keep inheritance?
constructor, function, javascript, oop, prototypal-inheritance
Solution
function F() {
var r = function() {
return {};
};
r.__proto__ = this.__proto__;
return r;
}
var f = new F();
f instanceof F;
true
f();
Object
Only works in the browsers with `__proto__`
Problem
``` function F() { return function() { return {}; } } var f = new F(); f instanceof F; // returns false ``` As far as I understand, if I want `instanceof` to work, I need to return `this` from the constructor. But I want the constructor to return a function, and I cannot assign to `this`. So, is this really impossible or can it be done somehow, for `f = new F()` to return a function and still `f instanceof F` to return true?