2016-09-27 123 views
0

我是打字稿和網頁設計領域的全新人物,所以爲了打開門,我正在編寫一個程序,在加載和重新加載時,它會輸出一個隨機車。一個例子輸出將是以下幾點:門在打字稿中定義打印的類型

編號:缸4數:4類型的傳輸:車身的FWD類型:SUV

有你能得到其他的結果,但這只是佈局的一個例子。

我在我的打字稿文件中的代碼如下:

class Car { 
    car: string; 
    door: number; 
    cylinder: number; 
    transmission: string; 
    body: string; 
    element: HTMLElement; 

    constructor(door: number, cylinder: number, transmission: string, body: string) { 
     this.car = door + " " + cylinder + " " + transmission + " " + body; 
    } 

} 

interface FullCar { 
    door: number; 
    cylinder: number; 
    transmission: string; 
    body: string; 
} 

function randomCarGenerator(car: FullCar) { 

    return "Number of Doors: " + car.door + "Number of Cylinders: " + car.cylinder + "Type of Transmission: " + car.transmission + "Type of Car Body: " + car.body; 

} 

var randomBody = ['Coupe', 'Sedan', 'Wagon', 'SUV']; 
var randomDoor1 = [2, 4, 5]; 
var randomDoor2 = [2, 4]; 
var randomCylinder = [2, 3, 4]; 
var randomTransmission1 = ['AWD', 'Standard', 'FWD']; 
var randomTransmission2 = ['Standard', 'FWD']; 

var body = randomBody[Math.floor(Math.random() * randomBody.length)]; 
if (body == 'Coupe') { 
    var door = 2; 
    var cylinder = randomCylinder[Math.floor(Math.random() * randomCylinder.length)]; 
    var transmission = randomTransmission2[Math.floor(Math.random() * randomTransmission2.length)]; 

    var car = new Car(door, cylinder, transmission, body); 
    document.body.innerHTML = randomCarGenerator(car); 
} 
else if (body == 'Sedan' || body == 'Wagon') { 
    var door = randomDoor1[Math.floor(Math.random() * randomDoor1.length)]; 
    var cylinder = randomCylinder[Math.floor(Math.random() * randomCylinder.length)]; 
    var transmission = randomTransmission1[Math.floor(Math.random() * randomTransmission1.length)]; 

    var car = new Car(door, cylinder, transmission, body); 
    document.body.innerHTML = randomCarGenerator(car); 
} 
else { 
    var door = randomDoor2[Math.floor(Math.random() * randomDoor2.length)]; 
    var cylinder = randomCylinder[Math.floor(Math.random() * randomCylinder.length)]; 
    var transmission = randomTransmission1[Math.floor(Math.random() * randomTransmission1.length)]; 

    var car = new Car(door, cylinder, transmission, body); 
    document.body.innerHTML = randomCarGenerator(car); 

} 

當我運行它,我得到的結果是門

編號:未定義缸未定義的數字等等等等

我的問題:我是如何聲明聲明值的問題?還是有一個完全不同的問題,我沒有看到?

回答

0
class Car { 
    car: string; 
    door: number; 
    cylinder: number; 
    transmission: string; 
    body: string; 
    element: HTMLElement; 

    constructor(door: number, cylinder: number, transmission: string, body: string) { 
     this.car = door + " " + cylinder + " " + transmission + " " + body; 
    } 

} 

在這裏,您:

  1. 定義了一堆領域對類構造函數的參數
  2. 採取具有相同名稱
  3. 設置類字段
  4. 全部留給之一未初始化的其他類字段

然後在這個fu節

function randomCarGenerator(car: FullCar) { 

    return "Number of Doors: " + car.door + "Number of Cylinders: " + car.cylinder + "Type of Transmission: " + car.transmission + "Type of Car Body: " + car.body; 

} 

您讀取所有你永遠不會初始化的字段。

你可以通過初始化場解決這個問題:

class Car { 
    car: string; 
    door: number; 
    cylinder: number; 
    transmission: string; 
    body: string; 
    element: HTMLElement; 

    constructor(door: number, cylinder: number, transmission: string, body: string) { 
     this.car = door + " " + cylinder + " " + transmission + " " + body; 
     this.door = door; 
     this.cylinder = cylinder; 
     // etc for the other fields 
    } 

} 

在打字稿一個很好的語法快捷方式是你可以使用訪問修飾符(如public)自動進行這些領域:

class Car { 
    car: string; 
    // Unused, not sure what you intend to do with this one 
    element: HTMLElement; 

    constructor(public door: number, public cylinder: number, public transmission: string, public body: string) { 
     this.car = door + " " + cylinder + " " + transmission + " " + body; 
    } 

} 
+0

這非常非常謝謝你! – user2580