为了账号安全,请及时绑定邮箱和手机立即绑定

SyntaxError:意外的标识符节点 js

SyntaxError:意外的标识符节点 js

梦里花落0921 2022-07-08 17:45:56
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
查看完整描述

2 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

您正在尝试像运行 JavaScript 一样运行 TypeScript 文件。JavaScript 和 TypeScript 不一样,node只能理解 JavaScript,不能理解 TypeScript。

您需要安装软件包typescriptts-node. 您可以在全局范围内这样做,以便您可以在任何地方使用它,用于单个文件:

npm i -g typescript ts-node

之后,您可以使用ts-node而不是node运行您的文件:

ts-node myScript.ts

这会将您的 TypeScript 文件即时编译为 JavaScript 并node为您运行结果。


查看完整回答
反对 回复 2022-07-08
?
噜噜哒

TA贡献1784条经验 获得超7个赞

我很确定要么存在格式问题,要么您使用的编译器不喜欢语法,但这是我在 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);

在我提到的链接中运行上面的代码,点击“运行”并点击“F12”打开控制台,你会看到console.log的输出


查看完整回答
反对 回复 2022-07-08
  • 2 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信