ต้องการต่อข้อความ method จาก superclass

พูดคุยแลกเปลี่ยน ปัญหา การเขียน JavaScript เครื่องมือ AJAX Web 2.0 AJAX Framework jQuery และ Node.JS รวมถึง Framework Express ของ Node.JS ทำงานฝั่ง Server

Moderator: mindphp

mjuangels

ต้องการต่อข้อความ method จาก superclass

โพสต์ที่ยังไม่ได้อ่าน โดย mjuangels »

โค้ดที่เขียนนะครับ !!

var count = 0;
var counts = {};
function Bicycle(cadence,gear,speed){
this.cadence = cadence;
this.gear = gear;
this.speed = speed;
count++;
}

Bicycle.prototype = {
printStates:function(){console.log("cadence:" + this.cadence + " speed:" + this.speed + " gear:" + this.gear);},
applyBrake:function(decrement){this.speed -= decrement;},
speedUp:function(increment){this.speed += increment;},
setCadence:function(cadence){this.cadence=cadence;},
setGear:function(gear){this.gear=gear;},
setSpeed:function (speed){this.speed=speed;},

}

Bicycle.prototype.printTotalBike = function(){
console.log("There are " + count + " bike.");
}

MountainBike.prototype = Object.create(Bicycle.prototype);
TandemBike.prototype = Object.create(Bicycle.prototype);

function MountainBike(startCadence,startSpeed,startGear,startHeight) {
Bicycle.call(counts,startCadence,startSpeed,startGear);
this.cadence = startCadence;
this.speed = startSpeed;
this.gear = startGear;
this.setHeight = startHeight;

this.setHeight = function(height){
this.height=height;
};

this.printStates = function(){
console.log("and height:" + this.height);
};
}

function TandemBike(startCadence,startSpeed,startGear,seat) {
Bicycle.call(counts,startCadence,startSpeed,startGear);
this.cadence = startCadence;
this.speed = startSpeed;
this.gear = startGear;
this.numOfSeat = seat;

this.setNumOfSeat = function(numOfSeat) {
this.numOfSeat = numOfSeat;
};

this.printStates = function(printStates){
console.log("and numOfSeat:" + this.numOfSeat);
};
}

var mb = new MountainBike(0, 0, 1, 0);
mb.setCadence(10);
mb.setSpeed(20);
mb.setGear(4);
mb.setHeight(100);
mb.printStates();

var tb = new TandemBike(0, 0, 1, 2);
tb.setCadence(15);
tb.setSpeed(40);
tb.setGear(5);
tb.printStates();

Bicycle.prototype.printTotalBike();

/* ผลลัพธ์ */

and height:100
and numOfSeat:2
There are 2 bike.
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 64