Javascript 删除以避免递归

Javascript 删除以避免递归,javascript,recursion,Javascript,Recursion,我一直在读“Javascript:Definitive Guid”,其中一个例子让我感到困惑。为什么处理IE的代码需要“this.onpropertychange=null”以避免递归。递归将如何发生?为什么我们不需要像对待其他浏览器一样对待函数“upcase”?多谢各位 //Example 17-7. Using the propertychange event to detect text input function forceToUpperCase(element) { if (

我一直在读“Javascript:Definitive Guid”,其中一个例子让我感到困惑。为什么处理IE的代码需要“this.onpropertychange=null”以避免递归。递归将如何发生?为什么我们不需要像对待其他浏览器一样对待函数“upcase”?多谢各位

//Example 17-7. Using the propertychange event to detect text input
function forceToUpperCase(element) {
    if (typeof element === "string") element = document.getElementById(element);
    element.oninput = upcase;
    element.onpropertychange = upcaseOnPropertyChange;
    // Easy case: the handler for the input event
    function upcase(event) {
        this.value = this.value.toUpperCase();
    }
    // Hard case: the handler for the propertychange event
    function upcaseOnPropertyChange(event) {
        var e = event || window.event;
        // If the value property changed
        if (e.propertyName === "value") {
            // Remove onpropertychange handler to avoid recursion
            this.onpropertychange = null;
            // Change the value to all uppercase
            this.value = this.value.toUpperCase();
            // And restore the original propertychange handler
            this.onpropertychange = upcaseOnPropertyChange;
        }
    }
}

因为当事件处理程序更改“value”属性时,它将触发另一个“propertychange”事件。因此,一旦第一个事件处理程序完成,浏览器将立即再次调用它

(在这种情况下,它不是真正的“递归”,它只是一个无限循环。)


第一个“简单”处理程序响应“输入”事件,更新“值”属性不会触发这些事件之一。但是,它将触发一个“propertychange”事件,因此在该处理程序中也执行相同的操作不会有任何伤害。

您了解
onpropertychange
的功能吗?不,我找不到IE事件对象属性的参考。你能详细说明一下吗?此外,还有一个“propertyName”。我想这也是IE特有的。谢谢你,我想我明白了。