如何清除object.key的编辑器错误 [英] How to get rid of editor’s error for object.key

查看:91
本文介绍了如何清除object.key的编辑器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,这些代码基本上获取一些JSON数据,使用"servergenre"查找键,然后将结果保存在数组中. 这是键":无法解决的变量或类型键

  • " => ":需要表达
  • "if(/服务器...":预期的正式参数名称
  • ")){":,预期
  • "}); ":需要声明
  • 这是一张图片,向您显示错误的位置:

    正如我所说的那样,代码可以正常工作,我只需要对其进行修复或采用其他方法来消除错误即可.

    此外,许多压缩器和压缩器不支持此代码位.所以我无法将其缩小.

    谢谢.

    解决方案

    ES2015(以前称为ES6)是JavaScript的更新版本,其中引入了诸如所使用函数的=>语法之类的功能. >

    并非所有浏览器都完全支持ES2015的所有功能,因此许多使用它的人首先将其通过编译器("transpiler")传递,然后将其转换为所有浏览器都支持的ES5. Babel 就是这样一种编译器.但是,如果您只针对较新的浏览器,则可以解释=>语法为何对您有用.

    您只需要更改您的编辑器设置即可了解该语法.确切的操作方式取决于您使用的文本编辑器.可能是您的编辑器的内置JavaScript模式不知道如何读取ES2015语法,因此您需要升级编辑器或安装提供更新的错误检查器的第三方插件.也可能是您的编辑器同时支持ES5和ES2015,并且认为您正在尝试仅在ES5中编写项目.在这种情况下,您只需要转到设置并告诉编辑器,您的意思是该项目使用ES2015(或ES2016,当前是最新版本).

    I have the following code that basically gets some JSON data, looks for the keys with "servergenre", and saves the results in an array. This is a follow up of this question.

    let result = [];
            Object.keys(data).forEach( key => {
                if(/servergenre/.test(key)){
                    result.push(data[key])
                }
            });
    

    Even though the code is working correctly, in some editors it raises syntactic errors:

    • "key": unsolvable variable or type key
    • "=>": expression expected
    • "if( / server...": formal parameter name expected
    • ")){": , expected
    • "});": statement expected

    Here is an image to show you where the errors are:

    As I said the code is working fine, I just need it to be fixed or another approach to get rid of the errors.

    Furthermore, many compressors and minifiers do not support this bit of code. So I can’t minify it.

    Thanks in advance.

    解决方案

    ES2015, formerly known as ES6, is a more recent version of JavaScript, which introduces features such as the => syntax for functions you are using.

    Not all features of ES2015 are fully supported in all browsers, so many people who use it pass it through a compiler ("transpiler") first to convert it to ES5, which is supported by all browsers. Babel is one such transpiler. But if you are only targeting newer browsers, that would explain why the => syntax is working for you.

    You just need to change your editor settings to understand that syntax. Exactly how you do that depends on what text editor you are using. It could be that your editor's built-in JavaScript mode doesn't know how to read ES2015 syntax, and you need to either upgrade your editor or install a third-party plugin that provides an updated error-checker. Or it could be that your editor supports both ES5 and ES2015, and it thinks you are trying to write your project only in ES5. In this case you just need to go to the settings and tell your editor that you mean for this project to use ES2015 (or ES2016, which is currently the most recent version).

    这篇关于如何清除object.key的编辑器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆