Tag: 继承

0

类的继承

混合复制 //复制source对象的属性 function mixin(source, target) { for (var key in source) { if (!(key in target)) { target[key] = source[key]; } } return target; } var Vehicle = { engines: 1, ignition: function() { console.log(" Turning on my forward! "); }, drive: function() { this.ignition(); console.log(" Steering and moving forward! "); } }; //Car复制Vehicle的属性值和函数引用 var Car = mixin(Vehicle, { wheels: 4, drive: function() { //显示指定Vehicle对象调用dirve方法 //如果直接执行Vehicle.dirve(),函数调用中的this就会绑定到Vehicle上,所以使用.call(this)来确保dirve在Car对象的上下文中执行; Vehicle.drive.call(this); console.log(" Rolling on all " + this.wheels + "wheels!"); } }); Car.drive();