原型问题2—原型对象的替换
Posted jokes
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原型问题2—原型对象的替换相关的知识,希望对你有一定的参考价值。
function Animal(){ this.type = "Animal"; } Animal.prototype.say = function(){ console.log(this.type); }
function Cat(){ this.vioce = "喵喵喵"; } Cat.prototype = new Animal(); Cat.prototype = { //这样会使上一条语句失效,从而使原型链断开。
shout:function(){ console.log(this.vioce); } }
Cat.prototype为什么会失效:
因为{}是一个新的对象,所以Cat.prototype的旧原型链就会断掉
以上是关于原型问题2—原型对象的替换的主要内容,如果未能解决你的问题,请参考以下文章