学习笔记-------JavaScript的继承

xiaoxiao2021-02-28  49

//原型 function fruit(name){ this.name = name; } //原型的方法 fruit.prototype.say = function(type){ return "I am " + this.name +", and I am " + type + "!!" }; //子类继承原型 function apple(name,type){ //通过CALL方法继承父类的name属性。 fruit.call(this,name); //子类自有属性 this.type = type; } //设置原型,继承原型的方法 apple.prototype = new fruit(); // 设置原型的构造器 apple.prototype.constructor = apple; //测试代码 alert(new apple("apple","sweet").say(new apple("apple","sweet").type));
转载请注明原文地址: https://www.6miu.com/read-2621372.html

最新回复(0)