Can JavaScript constructor return function and keep inheritance?

后端 未结 4 994
滥情空心
滥情空心 2021-01-01 17:56
function F() {
    return function() {
        return {};
    }
}

var f = new F();
f instanceof F; // returns false

As far as I understand, if I w

4条回答
  •  情深已故
    2021-01-01 18:29

    You could of course make all functions appear to be instanceof F by setting F.prototype = Function.prototype;.

    Unfortunately it looks as if ECMAScript doesn't allow you to create a function subclass.

    You can however do it in Gecko using the deprecated __proto__ property:

    function F() {
        function f() {
            return {};
        }
        f.__proto__ = F.prototype;
        return f;
     }
     F.prototype.__proto__ = F.__proto__;
    

提交回复
热议问题