SyntaxError:意外的标识符节点 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);

所以当我尝试编译时它说


private x: number;

            ^


SyntaxError: Unexpected identifier


梦里花落0921
浏览 120回答 2
2回答

一只斗牛犬

您正在尝试像运行 JavaScript 一样运行 TypeScript 文件。JavaScript 和 TypeScript 不一样,node只能理解 JavaScript,不能理解 TypeScript。您需要安装软件包typescript和ts-node.&nbsp;您可以在全局范围内这样做,以便您可以在任何地方使用它,用于单个文件:npm&nbsp;i&nbsp;-g&nbsp;typescript&nbsp;ts-node之后,您可以使用ts-node而不是node运行您的文件:ts-node&nbsp;myScript.ts这会将您的 TypeScript 文件即时编译为 JavaScript 并node为您运行结果。

噜噜哒

我很确定要么存在格式问题,要么您使用的编译器不喜欢语法,但这是我在 https://www.typescriptlang.org/play/中执行的代码 ,它工作得非常好。&nbsp; &nbsp; class Point {&nbsp;&nbsp; &nbsp; private x: number;&nbsp; &nbsp; private y: number;&nbsp; &nbsp; constructor(x: number, y: number) {&nbsp; &nbsp; this.x = x;&nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; public distance(otherPoint: Point): number {&nbsp; &nbsp; return Math.sqrt(&nbsp; &nbsp; &nbsp; &nbsp; Math.pow(this.x - otherPoint.getX(), 2) +&nbsp; &nbsp; &nbsp; &nbsp; Math.pow(this.y - otherPoint.getY(), 2));&nbsp; &nbsp; }&nbsp; &nbsp; public getX() {&nbsp; &nbsp; return this.x;&nbsp; &nbsp; }&nbsp; &nbsp; public getY() {&nbsp; &nbsp; return this.y;}}class Circle {&nbsp;&nbsp; &nbsp; private center: Point;&nbsp; &nbsp; private radius: number;&nbsp; &nbsp; constructor(center: Point, radius: number) {&nbsp; &nbsp; this.radius = radius;&nbsp; &nbsp; this.center = center;&nbsp; &nbsp; }&nbsp; &nbsp; public isInside(otherPoint: Point): boolean {&nbsp; &nbsp; return this.center.distance(otherPoint) < this.radius;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}class Triangle extends Point {&nbsp; &nbsp; private z: number;&nbsp; &nbsp; constructor(x:number, y: number, z: number) {&nbsp; &nbsp; &nbsp; &nbsp; super(x, y);&nbsp; &nbsp; &nbsp; &nbsp; this.z = z;&nbsp; &nbsp; }&nbsp; &nbsp; public getZ() {&nbsp; &nbsp; &nbsp; &nbsp; return this.z&nbsp; &nbsp; }&nbsp; &nbsp; public getPerimeter(otherPoint: Triangle): number {&nbsp; &nbsp; &nbsp; &nbsp; return otherPoint.getX() + otherPoint.getY() + otherPoint.getZ()&nbsp; &nbsp; }}let per = new Triangle(24, 61, 32);console.log(per);在我提到的链接中运行上面的代码,点击“运行”并点击“F12”打开控制台,你会看到console.log的输出
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript