当我将参数传递到console.log()
内的undefined
时,结果会回来var Bike = function() {
var gear = 2; // private var set to an arbitrary number
this.setGear = function(change) { // public method
gear = change;
};
this.getGear = function() { // public method
return gear;
};
};
var myBike = new Bike();
console.log(myBike.setGear(4)); // returns undefined, should return 4
console.log(myBike.setGear(3)); // returns undefined, should return 3
console.log(myBike.setGear(1)); // returns undefined, should return 1
,为什么会这样?结果应该是我传入的数字。
{{1}}
答案 0 :(得分:0)
this.setGear = function(change) { // public method
gear = change;
};
不应该返回4.
this.setGear = function(change) { // public method
gear = change;
return gear;
};
应该
答案 1 :(得分:0)
如果你设置myBike.setGear,你应该调用myBike.getGear函数来返回齿轮;
myBike.setGear(4);
console.log(myBike.getGear);