JS 对象的继承

//定义父元素
function
Animal(name,kg){ this.name = name; this.kg = kg; }; Animal.prototype.say = function(){ alert('大家好-',this.name); } //子元素 function Dog(type){ this.type = type; } Dog.prototype.job = function(){ alert('我的种类是'+this.type+',专门看家') } //继承 var extend = function(sub,sup,q,w){ var objSup = new sup(q,w); sub.apply(sup); sub.prototype = Object.assign(sup.prototype,sub.prototype); sub.prototype.constructor = sub; } extend(Dog,Animal,'a','b'); var taidi = new Dog('泰迪'); console.log(taidi) taidi.job(); taidi.say()

 

posted @ 2021-01-30 16:36  Jantelope  阅读(37)  评论(0)    收藏  举报