Why this behaviour?__proto__ vs prototype?

前端 未结 3 883
南旧
南旧 2021-01-15 10:28
    function Obj1(name){
        this.__proto__={
            Name:name,
            getName:function(){
                alert(this.Name); 
            }


        }         


        
3条回答
  •  执笔经年
    2021-01-15 10:59

    Only functions has the property prototype. You need to set the prototype on the function self.

    function Obj2(name){
        this.name = name;
    }
    
    Obj2.prototype={
        getName:function(){
           alert(this.Name); 
        }
    };
    

提交回复
热议问题