简体   繁体   English

JavaScript-使用原型的构造函数

[英]JavaScript - Constructor using Prototype

I have a question relating with using if statement within my prototype constructor. 我有一个关于在原型构造函数中使用if语句的问题。

What I am trying to do: 我正在尝试做的是:

  1. Create a method calculatePrice for the Item constructor. 为Item构造函数创建一个calculatePrice方法。
  2. This method will return the price of the object by default. 默认情况下,此方法将返回对象的价格。
  3. Item is fruit, return the price of the Item minus 5%. 物品是水果,物品的价格减去5%。

Code ** NOT WORKING 代码**不工作

function Item(name, price){
  this.name = name;
  this.price = price;
}

Item.prototype.calculatePrice = function() {
  if (this.name === 'fruit') {
      this.price = this.price * 0.95
  } else {
      this.price = this.price;
  }
}

var ball = new Item('soccer ball', 15);
ball.calculatePrice();
// Expected results: 15

var fruit = new Item('fruit', 10);
fruit.calculatePrice();
// Expected results: 9.5

However my error is how I wrote the if statement. 但是我的错误是我如何编写if语句。 Without giving me the solution, could you please direct me on where my mistake was made? 如果不给我解决方案,您能否指导我犯错的地方? Thank you. 谢谢。

You didn't describe what are your expectations and actual outcome of your code. 您没有描述代码的期望值和实际结果。 But one thing that came to mind is, that you forgot to return this.price in your calculatePrice method, so result is undefined instead of price. 但是想到的一件事是,您忘记了在calculatePrice方法中返回this.price ,因此结果是undefined而不是price。 You get result even with your current version, but you have to do it explicitly by checking price property of an instance eg. 即使使用当前版本,您也可以获得结果,但是您必须通过检查实例的price属性来明确地做到这一点,例如。 fruit.price . fruit.price And also price will mutate with every calculatePrice method call. 而且价格将随着每次calculatePrice方法调用而发生变化。 Instead assign this.price to local variable, do the calculation on that variable and return it. 而是将this.price分配给局部变量,对该变量进行计算并返回。

The problem is that if you call the method repeatedly, the price will keep decreasing (for fruit). 问题是,如果您反复调用该方法,则价格将继续下降(对于水果)。

Also the else clause is doing nothing really: you assign a value that was already assigned. else子句实际上并没有做任何事情:您分配一个已经分配的值。

Instead of storing the calculation result back in this.price , return it as the function result. 而不是将计算结果存储回this.price ,而是将其作为函数结果返回 That way this.price remains the same (no unexpected side effects), and can be controlled by the user of the object. 这样, this.price保持不变(没有意外的副作用),并且可以由对象的用户控制。 The method then only returns the result: 然后,该方法仅返回结果:

var result = fruit.calculatePrice();

Now fruit.price will still be the original 10, but result will be 9.5 现在fruit.price仍将是原始的10,但result将是9.5

Optionally, you could let the function store the result also as an object property, but then it should better be a different one (eg this.calculatedPrice ). (可选)您可以让函数也将结果存储为对象属性,但最好是将其作为另一个对象(例如this.calculatedPrice )。

NB: As requested, the actual solution code is not provided. 注意:根据要求,未提供实际的解决方案代码。 Let me know if you need more. 让我知道您是否需要更多。

UPDATE: Working Code: 更新:工作代码:

function Item(name, price){
  this.name = name;
  this.price = price;
}

Item.prototype.calculatePrice = function() {
  if (this.name === 'fruit') {
      return 0.95 * this.price;
 } else {
      return this.price;
  }
}

var ball = new Item('soccer ball', 15);
ball.calculatePrice();
// => 15

var fruit = new Item('fruit', 10);
fruit.calculatePrice();
// => 9.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM