Javascript 如何使构造函数内的方法引用其参数?

Javascript 如何使构造函数内的方法引用其参数?,javascript,Javascript,我被要求执行构造车的getSpeed、accelerate和Decreate方法。调用这些方法时,速度会相应地变化。我试过这个。速度+/-加速度或参数,但仍然不起作用 var Car = function (acceleration) { var speed = 0; this.getSpeed = function () { return speed; } this.accelerate = function () { return this.speed

我被要求执行构造车的getSpeed、accelerate和Decreate方法。调用这些方法时,速度会相应地变化。我试过这个。速度+/-加速度或参数,但仍然不起作用

var Car = function (acceleration) {

  var speed = 0;

  this.getSpeed = function () {
    return speed; 
  }

  this.accelerate = function () {
    return this.speed + arguments;
  }

  this.decelerate = function () {
    return this.speed - arguments;

  }
}

var honda = new Car(10);
console.log(honda.getSpeed()); // -> 0

honda.accelerate(); 
console.log(honda.getSpeed());// -> 10


honda.accelerate();
console.log(honda.getSpeed());// -> 20


honda.decelerate();
console.log(honda.getSpeed());// -> 10

您没有为speed go指定新值,而是使用-=和+=而不是-和+

  this.getSpeed = function () {

   return speed; 
  }

  this.accelerate = function () {

  return speed += acceleration;
  }

  this.decelerate = function () {

  return speed -= acceleration;

  }

有两个错误,第一个错误是使用var声明表示速度,但在方法中尝试通过this.speed访问它。 第二,在加速方法中,你需要改变这个值this.speed+=this.acceleration


你搞错了。当你加速1次后,它会返回10,但在那之后它会一次又一次地返回10。如果你用减速,它会变成-10。如果您再次使用它,它将再次转到-10


var Car = function (acceleration) {

  this.acceleration = acceleration;
  this.speed = 0;

  this.getSpeed = function () {

    return this.speed; 
  }

  this.accelerate = function () {
    return this.speed += this.acceleration;
  }

  this.decelerate = function () {
    return this.speed -= this.acceleration;
  }
}

var honda = new Car(10);

console.log(honda.getSpeed()); // -> 0


honda.accelerate();

console.log(honda.getSpeed());// -> 10


honda.accelerate();

console.log(honda.getSpeed());// -> 20


honda.decelerate();

console.log(honda.getSpeed());// -> 10
之后,它先从0设置,然后设置为10,再设置为10,依此类推

您还忘记将此.speed设置为0。您只使用了var speed=0

对于Acceleration,您必须在函数中使用它,否则将无法定义它


所以基本上你错了三件事

我认为这是正确的做法。将方法放入原型中,以便下次实例化汽车构造函数时,它不会重新创建这些方法,而是引用其原型链

const Car = function(speedo){
     this.speed = 0;
     this.speedo = speedo
}

Car.prototype.getSpeed = function () {
    return this.speed;
}

Car.prototype.accelerate = function () {
    this.speed += this.speedo;
  }

Car.prototype.decelerate = function () {
    this.speed -= this.speedo;
  }


var myAwesomeFreakNew = new Car(1000);
console.log(myAwesomeFreakNew.getSpeed())

myAwesomeFreakNew.accelerate();
console.log(myAwesomeFreakNew.getSpeed())

myAwesomeFreakNew.accelerate();
console.log(myAwesomeFreakNew.getSpeed())

myAwesomeFreakNew.decelerate();
console.log(myAwesomeFreakNew.getSpeed())

参数是传递给函数调用的值的类似数组的集合。你不能只是在速度上加/减它。而且,这个.speed根本没有定义。您可能分别需要speed+=参数[0]和speed-=参数[0]。欢迎。我们希望你能接受这个问题的最佳答案,或者是解决和解释你问题的答案。不,不是。您只为构造函数定义了速度,而没有为其他函数定义速度。所以没有这个速度。这也会产生错误。忘了做this.speed和this.speedo我的坏将编辑
const Car = function(speedo){
     this.speed = 0;
     this.speedo = speedo
}

Car.prototype.getSpeed = function () {
    return this.speed;
}

Car.prototype.accelerate = function () {
    this.speed += this.speedo;
  }

Car.prototype.decelerate = function () {
    this.speed -= this.speedo;
  }


var myAwesomeFreakNew = new Car(1000);
console.log(myAwesomeFreakNew.getSpeed())

myAwesomeFreakNew.accelerate();
console.log(myAwesomeFreakNew.getSpeed())

myAwesomeFreakNew.accelerate();
console.log(myAwesomeFreakNew.getSpeed())

myAwesomeFreakNew.decelerate();
console.log(myAwesomeFreakNew.getSpeed())