gpt4 book ai didi

javascript - TypeScript 中未定义的属性

转载 作者:行者123 更新时间:2023-11-28 02:30:13 26 4
gpt4 key购买 nike

我在事件方面遇到问题。

我创建了两个界面:

export interface IEEvent extends JQueryEventObject, MSEventObj {
preventDefault: () => void;
cancelBubble: bool;
}

export interface IEElement extends HTMLElement {
click: (event?: IEEvent) => void;
onmousedown: (event?: IEEvent) => void;
onmousemove: (event?: IEEvent) => void;
onmouseup: (event?: IEEvent) => void;
}

我尝试设置 onmousedown\move\up 属性,但收到错误...

public static StopPropagation(element: HTMLElement): void {
(<IEElement> element).onmousedown = StopPropagationHandler; // error here
(<IEElement> element).click = StopPropagationHandler; // error here
(<IEElement> element).onmouseup = StopPropagationHandler; // error here
}

private static StopPropagationHandler(e?: IEEvent): void {
if (typeof (e) === "undefined") {
e = <IEEvent> window.event;
}
if (typeof (e.preventDefault()) !== "undefined") { // error here
e.preventDefault(); // error here
}
e.cancelBubble = true;
}

Properties

Method

如何消除这些错误?

最佳答案

听起来好像当异常发生时 StopPropagation 位于调用堆栈的顶部。如果您在调试器内部检查它,我怀疑元素将是未定义的。您可以考虑修改代码来处理此问题,或者在这种情况下抛出异常。示例:

允许取消定义:

public static StopPropagation(element: HTMLElement): void {
if (element) {
(<IEElement> element).onmousedown = StopPropagationHandler; // error here
(<IEElement> element).click = StopPropagationHandler; // error here
(<IEElement> element).onmouseup = StopPropagationHandler; // error here
}
}

禁止取消定义:

public static StopPropagation(element: HTMLElement): void {
if (!element) {
throw new Error("Null argument exception. An element must be provided.");

(<IEElement> element).onmousedown = StopPropagationHandler; // error here
(<IEElement> element).click = StopPropagationHandler; // error here
(<IEElement> element).onmouseup = StopPropagationHandler; // error here
}

关于javascript - TypeScript 中未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14341244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com