求解 为什么不能通过
Challenge Make Object Properties Private has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
var Car = function() {
// this is a private variable
var speed = 10;
// these are public methods
this.accelerate = function(change) {
speed += change;
};
this.decelerate = function() {
speed -= 5;
};
this.getSpeed = function() {
return speed;
};
};
var Bike = function() {
// 只能在这一行下面写代码
var gear=0;
this.setGear=function(num){
gear+=num;
};
this.getGear=function(){
return gear;
};
};
var myCar = new Car();
var myBike = new Bike();

var Bike = function() {
// 只能在这一行下面写代码 var gear=0; var igear; this.setGear=function(num){ igear=gear+num ; };
this.getGear=function(){ return igear; };
}; var myCar = new Car(); var myBike = new Bike(4); 改成这样可通过 你的函数最后输出的并不是加了以后的gear,而是外部的定义的gear,依旧等于0。
@zhangwenhui123 写法并不用你这么麻烦,igear是多余的。
var Bike = function() {
var gear = 0;
this.setGear = function(num) {
gear = num;
};
this.getGear = function() {
return gear;
};
};
var myCar = new Car();
var myBike = new Bike(4);
这题做错的地方在setGear直接写 = 就好,但是却写了 +=。