Syntax error using => in IE [duplicate]

試著忘記壹切 提交于 2021-02-20 18:58:15

问题


I have the following line of javascript code

var res = Object.keys(packages).filter(e => packages[e] === true)

The above works well in all the other browser apart from IE. IE complains about Syntax erro at => can someone tell me how to get around this in IE


回答1:


IE must not support arrow-functions. Just use the old function keyword.

.filter(function(e){ return packages[e] === true })

Side note, but you could also probably write this as:

.filter(function(e){ return packages[e] })

Unless packages[e] must actually be exactly equal to true and not just truthy.




回答2:


IE doesn't support the fat arrow notation (Edge does). See http://kangax.github.io/compat-table/es6/. You need the older function notation:

var res = Object.keys(packages).filter(function(e) { return packages[e] === true })



回答3:


CanIUse: Arrow function You cannot use it in IE;

var res = Object.keys(packages).filter(function(e) = {return packages[e] === true})


来源:https://stackoverflow.com/questions/44437497/syntax-error-using-in-ie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!