用 JavaScript 建立一個投射物類來計算高度、水平距離和著陸點


問題

我們需要編寫一個 JavaScript 類:Projectile,該類在初始化時接收 3 個引數:-

  • 起始高度 (0 ≤ h0 < 200)
  • 起始速度 (0 < v0 < 200)
  • 投射物的釋放角 (0° < a < 90°,以度為單位)。

我們需要為 Projectile 類編寫以下方法。

  • 一個 horiz 方法,該方法接收引數 t,並計算投射物水平位移。(接收一個 double,返回一個 double)

示例

此類程式碼如下:

 線上示例

class Projectile{
   constructor(h, u, ang){
      this.h = h;
      this.u = u;
      this.ang = ang;
   };
};
Projectile.prototype.horiz = function(t){
   const dist = 2 * Math.cos(this.ang) * t;
   return dist;
};
const p = new Projectile(5, 2, 45);
const horizontal = p.horiz(.2);
console.log(horizontal);

輸出

輸出如下:

0.2101287955270919

更新時間: 2021-04-20

103 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.