我必須通過原型添加屬性和方法。我有我的代碼工作順序,但沒有基於用戶輸入的屬性值,它顯示bikeArray [0] .color未定義。我不認爲我正確調用方法功能,或者我沒有正確定義它。我有點困惑,我在這裏做錯了什麼。通過原型問題添加方法javascript
HTML:
<div id="requirement_13">
<h3>Requirment 13 - 14</h3>
<p id="bike"></p>
<ul id="speed_info">
<li>Our bike gears can be customized to achieve certain speed possibilities.</li>
<li>The standard gearing speed is 12mph.</li>
<li>If you would like something different, please select a number below to either<br>
add or subtract from max gear speed.</li>
<li>We will handle the rest from there.<br></li>
</ul>
<form>
<input type="number" id="speed" min="-6" max="15" required>
</form>
<ul>
<li> Our standard bike color is "Gun Metal Grey"</li>
<li> You can select a bike color from below if<br>
you wish to change the color.</li>
</ul>
<form id="bike_color">
<select name="change_color:">
<option value="Gun Metal Grey" selected>Gun Metal Grey</option>
<option value="Cherry Red">Cherry Red</option>
<option value="Sky Runner Blue">Sky Runner Blue</option>
<option value="Rose Pink">Rose Pink</option>
<option value="Plum Purpler">Plum Purpler</option>
</select>
<br>
<br>
<input type="button" id="gear_submit" value="Submit" onclick="createBike();">
<p id="show_bike"></p>
</form>
</div>
的JavaScript:
//global bike array
var bikeArray = [];
function createBike() {
function bike(model, speed) {
this.model = model;
this.speed = speed;
// this will change speed based on user input
this.changeSpeed = function (changeSpeed) {
var new_speed = parseInt(document.getElementById("speed").value, 10);
bikeArray[0].speed = speed + new_speed;
}
}
var bike1 = new bike("Ghost Ryder", 12);
bikeArray[0] = bike1;
bike1.changeSpeed();
// add the color property and method to set the value of color
bike.prototype.color;
bike.prototype.color = function (colorPicker){
var color = document.getElementsByName("change_color").value;
bike1.color = color;
}
bike1.color();
document.getElementById("show_bike").innerHTML =
bikeArray[0].model + " " + bikeArray[0].speed + " " + bikeArray[0].color;
}
我出去放看起來是這樣的:幽靈萊德14未定義
,其中不確定的是,我需要它來顯示用戶選擇顏色。
哪一部分的代碼返回未定義'?你能更精確嗎? – Lauromine
顏色屬性值返回undefined ...生病編輯我的帖子 – kronis72
'bike.prototype.color;'是什麼行? –