javascript hasOwnProperty and prototype

后端 未结 3 1714
旧时难觅i
旧时难觅i 2020-12-20 22:29
function Animal(name,numLegs){
this.name = name;
this.numLegs = numLegs}

Animal.prototype.sayName = function(){
console.log(\"Hi my name is \" + this.name );}

var          


        
3条回答
  •  没有蜡笔的小新
    2020-12-20 23:16

    Because 'sayname' is inherited from 'Animal' its not 'penguin' own property.

    check : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

    similar example :

    function Foo() {
      this.value = 'qwert';
    }
    Foo.prototype.say = "hello";
    
    var foo = new Foo();
    
    foo.hasOwnProperty("say"); // False, since say is inherited from the prototype object.
    foo.hasOwnProperty("value"); // True, since value is the property of foo itself.
    

提交回复
热议问题