在项目中看到这样一种阻止页面刷新/关闭的方法,我能知道它最终实现的效果但是不能理解它的写法。
在网上搜索Object.assign()
得到的是用于合并对象(例子),
在这里为什么要和e
绑定而且也没有变量接收这个合并值,就有点不能理解。
还请前辈指导一下 :-)
...
mounted() {
const returnValue = 'Are you sure you want to lose unsaved changes?';
window.onbeforeunload = e => {
if (!this.changedFiles.length) return undefined;
Object.assign(e, {
returnValue,
});
return returnValue;
};
},
...
Object.assign
在这里完全等价于
e.returnValue = returnValue
。returnValue
就是为了兼容性
https://developer.mozilla.org...