Why this behaviour?__proto__ vs prototype?

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


        }         


        
3条回答
  •  孤独总比滥情好
    2021-01-15 10:50

    __proto__ is not a standard property.

    Anyway every object created by new will get a prototype from the .prototype member of the constructor (a function). Note that the prototype member has no name, you cannot access it directly, you need Object.getPrototypeOf(x).

    If you want to create an object with a given prototype the code is Object.create(proto) that is more or less equivalent to

    function makeObjectWithPrototype(x) {
        function makeit() { }
        makeit.prototype = x;
        return new makeit();
    }
    

提交回复
热议问题