typescript产生的javascript报错 undefined property

ts代码

class Point {
    x: number;
    y: number;

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

产生的js代码

var Point = /** @class */ (function () {
    function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    return Point;
}());

在HTML文件中引用js代码,按如下方式使用

window.onload = function() {
    const point = Point(300, 300)
    console.log(point.x)
}

报错,TypeError: Cannot read property 'x' of undefined

已确认文件引用无误,因为可以访问到同一js文件中的其他对象。
请教一下这是什么问题。是我什么地方写错了吗?劳烦帮忙看一下,不胜感激。

阅读 2.3k
1 个回答

使用new Point(1,1)调用,你少了new

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题