Javascript Can';t从构造函数调用方法
从同一类中的构造函数调用private/public方法时出错 未捕获类型错误:this.setEventHandler不是函数 在Object.Init(Init.ts:4) 时间:3:16 这是已编译的javascriptJavascript Can';t从构造函数调用方法,javascript,typescript,Javascript,Typescript,从同一类中的构造函数调用private/public方法时出错 未捕获类型错误:this.setEventHandler不是函数 在Object.Init(Init.ts:4) 时间:3:16 这是已编译的javascript var Init = (function () { function Init(connectBtn) { this.connectBtn = connectBtn; this.setEventHandler(); } Init.prototype.set
var Init = (function () {
function Init(connectBtn) {
this.connectBtn = connectBtn;
this.setEventHandler();
}
Init.prototype.setEventHandler = function () {
var _this = this;
this.connectBtn.onclick = function (e) {
_this.openConnectWindow();
};
};
Init.prototype.openConnectWindow = function () {
console.log("OPEN SESAME");
};
return Init;
}());
编辑:这是一个库
我尝试放置var mylib=new MyLibraryName(),但只得到“MyLibraryName不是构造函数”
我用webpack启动根对象
output: {
path: path.join(__dirname, "lib"),
filename: outputFile,
library: libraryName,
libraryTarget: "umd",
umdNamedDefine: true
},
将您正在调用的方法从其他方法更改为此
export class Init {
constructor(private connectBtn: HTMLElement) {
this.setEventHandler();
}
public setEventHandler = () => {
this.connectBtn.onclick = (e) => {
this.openConnectWindow();
}
}
private openConnectWindow = () => {
console.log("OPEN SESAME")
}
}
尝试调试一点演示如何创建
Init
类实例它将成为一个库,因此我只需键入MyLibraryName.Initvar ele=document.getElementsByClassName('dropdown-toggle')[0];MyLibraryName.Init(ele)你能告诉我们为什么这是必要的吗?是的,它的工作原理很好,但我为什么要使用它?它改变了我的语法。我们有没有办法配置一些东西,使其在两种方式下都能工作?
export class Init {
constructor(private connectBtn: HTMLElement) {
this.setEventHandler();
}
public setEventHandler = () => {
this.connectBtn.onclick = (e) => {
this.openConnectWindow();
}
}
private openConnectWindow = () => {
console.log("OPEN SESAME")
}
}