简体   繁体   中英

SyntaxError: Unexpected identifier node js

class Point {

    private x: number;
    private y: number;

    constructor(x: number, y: number) {
      this.x = x;
      this.y = y;
    }

    public distance(otherPoint: Point): number {
      return Math.sqrt(
          Math.pow(this.x - otherPoint.getX(), 2) +
          Math.pow(this.y - otherPoint.getY(), 2));
    }

    public getX() {
      return this.x;
    }

    public getY() {
      return this.y;
    }

  }

  class Circle {

    private center: Point;
    private radius: number;

    constructor(center: Point, radius: number) {
      this.radius = radius;
      this.center = center;
    }

    public isInside(otherPoint: Point): boolean {
      return this.center.distance(otherPoint) < this.radius;
    }

  }

  class Triangle extends Point {
      private z: number;

      constructor(x:number, y: number, z: number){
          super(x, y);
          this.z = z;
      }

    public getZ(){
        return this.z
    }

    public getPerimeter (otherPoint: Triangle): number{
        return otherPoint.getX() + otherPoint.getY() + otherPoint.getZ()
    }
  }

  let per = new Triangle(24, 61, 32);
  console.log(per);

so when i try to compile it says

private x: number;
            ^

SyntaxError: Unexpected identifier

i am pretty sure either there is formatting issues or you are using a compiler which does not like the syntax, but here is the code i executed in https://www.typescriptlang.org/play/ and it worked perfectly fine.

    class Point { 
    private x: number;
    private y: number;

    constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
    }

    public distance(otherPoint: Point): number {
    return Math.sqrt(
        Math.pow(this.x - otherPoint.getX(), 2) +
        Math.pow(this.y - otherPoint.getY(), 2));
    }

    public getX() {
    return this.x;
    }

    public getY() {
    return this.y;
}
}

class Circle { 

    private center: Point;
    private radius: number;

    constructor(center: Point, radius: number) {
    this.radius = radius;
    this.center = center;
    }

    public isInside(otherPoint: Point): boolean {
    return this.center.distance(otherPoint) < this.radius;
    }    
}

class Triangle extends Point {
    private z: number;
    constructor(x:number, y: number, z: number) {
        super(x, y);
        this.z = z;
    }

    public getZ() {
        return this.z
    }

    public getPerimeter(otherPoint: Triangle): number {
        return otherPoint.getX() + otherPoint.getY() + otherPoint.getZ()
    }

}

let per = new Triangle(24, 61, 32);
console.log(per);

Run above code in the link that i mentioned, hit "Run" and hit "F12" to open the console, you will see the output from console.log

You are trying to run a TypeScript file as if it were JavaScript. JavaScript and TypeScript are not the same, and node can only understand JavaScript, not TypeScript.

You need to install the packages typescript and ts-node . You can do so globally so that you can use it everywhere, for individual files:

npm i -g typescript ts-node

Afterwards, you can use ts-node instead of node to run your file:

ts-node myScript.ts

This will compile your TypeScript file to JavaScript on the fly and run the result with node for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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