Minor drawback with Crockford Prototypical Inheritance

前端 未结 2 1811
离开以前
离开以前 2021-01-04 23:29

Just experimenting with different inheritance techniques in JS, and came across something mildly discomfiting about Crockford\'s Prototypal Inheritance pattern:



        
2条回答
  •  一向
    一向 (楼主)
    2021-01-04 23:46

    If I log the object I can see: Object { foo="bar", baz=function()}, so I don't understand your problem...

    Anyway, can use Object.create() instead of Crockford's function:

    var P = {
             foo:'bar',
             baz: function(){ alert("bang"); }
             }
    
    var C = Object.create (P);
    

    console.log (C):

    Object { foo="bar", baz=function()}

提交回复
热议问题