谷歌浏览器Chrome console.log 使用注意事项
微wx笑
2019-08-27【网络工具】
510
16
0关键字:
Chrome 谷歌浏览器
今天在使用谷歌浏览器Chrome调试的时候遇到了一个有意思的问题,断点监视时的变量值与console.log输出的不一样。
谷歌浏览器Chrome console.log 使用注意事项
今天在使用谷歌浏览器Chrome调试的时候遇到了一个有意思的问题,断点监视时的变量值与console.log输出的不一样。
场景:
网页添加了粘贴事件处理程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | document.addEventListener( 'paste' , function (e) { var items = ((e.clipboardData || window.clipboardData).items) || []; var file = null ; if (items && items.length) { for ( var i = 0; i < items.length; i++) { if (items[i].type.indexOf( 'image' ) !== -1) { file = items[i].getAsFile(); break ; } } } if (!file) { alert( '粘贴内容非图片!' ); return ; } console.log(e.clipboardData.items); console.log(e); }); |
可以看到后台的两句 console.log,问题就出在这里了。
可以看到,在函数内的时候,打印 e.clipboardData.items length 是 1,而当函数执行完毕,length 就变成了 0,
当函数执行完毕,变量的结构还在,但数据全部丢失了。
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/toolbox/newwork/2019-08-27/159.html