patchProp 函数在为元素添加监听函数时,是否需要先使用 createInvoker 函数对监听函数进行包装?

作者: nongsia创建于 2022年12月11日更新于 2022年12月11日

function patchProp(el, key, preValue, nextValue) { if (isOn(key)) { const invokers = el._vei || (el._vei = {}); const existingInvoker = invokers[key]; if (nextValue && existingInvoker) { existingInvoker.value = nextValue; //这个invoker是一个函数,直接修改function.value是不是有点问题?我没有找到类似的写法 } else { const eventName = (key).slice(2).toLowerCase(); if (nextValue) { const invoker = (invokers[key] = nextValue); //没有使用createInvoker包装函数来包装,所以invoker是一个函数 el.addEventListener(eventName, invoker); } else { el.removeEventListener(eventName, existingInvoker); invokers[key] = undefined; } } } else { if (nextValue === null || nextValue === "") { el.removeAttribute(key); } else { el.setAttribute(key, nextValue); } } }

内容来源: cuixiaorui/mini-vue