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文件中的其他对象。
请教一下这是什么问题。是我什么地方写错了吗?劳烦帮忙看一下,不胜感激。
使用
new Point(1,1)
调用,你少了new