__proto__, when will it be gone? Alternatives?

前端 未结 3 1977
-上瘾入骨i
-上瘾入骨i 2020-12-01 11:22

Mozilla claimed it would remove __proto__ a while back (~2008) and it is still in the browser. Is it still going to be deprecated? It works in Opera, (Safari I think) and Ch

3条回答
  •  有刺的猬
    2020-12-01 11:34

    I don't see how:

    var a = {};
    a.__proto__ = A; // A is object because no constructor is needed
    

    Is simpler than:

    var a = new A(); // A is Constructor function
    

    Now setting up A in the latter case can be more verbose if you don't need a constructor function, but in that case you could automate it to be just as simple:

    var A = Constructor({
        method: function(){}
    }); // A is function, meant to be used with new
    

    Compared to:

    var A = {
        method: function(){}
    }; //A is object, meant to be set as __proto__
    

    And if you need some initialization logic, it will probably end up being easier just to use the normal way where the constructor function needs to be declared anyway

提交回复
热议问题