简体   繁体   English

SyntaxError:意外的标识符节点 js

[英]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.我很确定要么存在格式问题,要么您使用的编译器不喜欢语法,但这是我在https://www.typescriptlang.org/play/中执行的代码,它工作得非常好。

    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在我提到的链接中运行上面的代码,点击“运行”并点击“F12”打开控制台,你会从console.log看到output

You are trying to run a TypeScript file as if it were JavaScript.您正在尝试运行 TypeScript 文件,就像它是 JavaScript 一样。 JavaScript and TypeScript are not the same, and node can only understand JavaScript, not TypeScript. JavaScript和TypeScript不一样, node只能理解JavaScript,不能理解Z558B544CF6839C39D3E

You need to install the packages typescript and ts-node .您需要安装软件包typescriptts-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而不是node来运行您的文件:

ts-node myScript.ts

This will compile your TypeScript file to JavaScript on the fly and run the result with node for you.这将在运行中将您的 TypeScript 文件编译为 JavaScript 并使用node为您运行结果。

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

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