前端生成二维码,网上大多数都在推荐qrcodejs,这个插件也确实好用,但是在一些使用不当的时候总会遇到莫名其妙的问题。例如,七娃最近苦恼的”Cannot read properties of undefined (reading '_android')“。

折磨了好几天的情况下,终于在官方的issues找到了解决办法:
二维码qrcodejs插件报错:Cannot read properties of undefined (reading ‘_android’)?-QUI-Notes
果真,七娃把 if (this._android && this._android <= 2.1) 改为 if (this && this._android <= 2.1) 代码就愉快的运行了。

问题原因:

在严格模式下("use strict";)会报错,因为此时this为undefined,而不是window。

记录一下这个问题吧,还是很有意义的,又学一个小知识点严格模式下的this指向不是window。