可能是你的init()方法写错了
所有慕课网注册用户在慕课网活动(回答问题、采纳答案、分享内容等)过程中均有机会获得积分,积分是一种荣誉的体现。积分可以用来购买道具甚至兑换实物礼品。慕课网会在未来逐步推出各类积分兑换内容,请随时关注关于慕课网积分的新消息吧。
new是实例化,实例化一遍原型对象,问题是你需要原型对象的内容全部吗?通过call继承是可以指定某个方法,当然,既然是父类构造函数通过原型链引用其父类的方法不是更省事吗?
this是dom对象,不能够使用jquery的方法。$(this)是把dom对象转换成了jquery的对象,然后可以使用jquery的方法。
LightEntire.prototype = Object.create(Light.prototype); LightEntire.prototype.constructor = LightEntire
Object.create()创建一个空对象,这个空对象的原型指向Light.prototype,将LightHalf的prototype=这个空对象的原型,实现一个继承关系,这里的construstor指向Light,可以修改成自身LightHalf
,如果直接LightHalf.prototype=Light.prototype,在修改LightHalf时就会影响到Light,因为他们指向同一个对象
原因找到了,superClass写错了。
这里就举这个coffe的例子了,应该是要先继承父类,再重写父类的方法。
var Beverage = function(){}; Beverage.prototype.boilWater = function(){ console.log("把水煮沸"); }; Beverage.prototype.brew = function(){ throw new Error("子类必须重写该方法"); }; Beverage.prototype.pourInCup = function(){ throw new Error("子类必须重写该方法"); }; Beverage.prototype.addCondiments = function(){ throw new Error("子类必须重写该方法"); }; Beverage.prototype.customerWantsCondiments = function(){ return true; }; Beverage.prototype.init = function(){ this.boilWater(); this.brew(); this.pourInCup(); if(this.customerWantsCondiments){ this.addCondiments(); } }; var Coffee = function(){}; Coffee.prototype = new Beverage();//应该是先继承父类,之后再重写方法 Coffee.prototype.boilWater = function(){ console.log("把水煮沸"); }; Coffee.prototype.brew = function(){ console.log("用沸水冲泡咖啡"); }; Coffee.prototype.pourInCup = function(){ console.log("把咖啡倒进杯子"); }; Coffee.prototype.addCondiments = function(){ console.log("加糖和牛奶"); }; var coffee = new Coffee(); coffee.init();
我感觉第一部也得消化好久,得来回多看几遍