【问题标题】:SyntaxError: Unexpected identifier node jsSyntaxError:意外的标识符节点 js
【发布时间】:2020-08-04 13:39:43
【问题描述】:
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

【问题讨论】:

  • 你是用 Typescript 编译的吗?
  • 是的,我们的工作主要是使用私有类型脚本来完成
  • 您正在使用 Typescript 语法编写代码,但我的意思是,您是使用 Typescript 编译器编译它还是使用 node 命令运行它?您能否发布一下您是如何运行代码的?
  • 我用节点命令 node oop.ts -> 文件名运行它
  • 好吧。你需要在你的机器上安装 Typescript 然后将你的 Typescript 代码编译成 Javascript ode 以便能够使用node oop.ts

标签: javascript node.js typescript


【解决方案1】:

您正在尝试像运行 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 的结果。

【讨论】:

  • 好的,所以我想我在哪里得到了创建 js 文件的命令是 npx tsc oop.ts 然后运行它我点击节点 oop.JS 对不起大家:D
【解决方案2】:

我很确定是否存在格式问题,或者您使用的编译器不喜欢该语法,但这是我在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的输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2017-10-11
    相关资源
    最近更新 更多