Can JavaScript constructor return function and keep inheritance?

后端 未结 4 992
滥情空心
滥情空心 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:24

    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__

提交回复
热议问题