简体   繁体   English

将 ES6 箭头函数转换为 ES5 函数 JavaScript

[英]Convert ES6 arrow function to ES5 function JavaScript

What is the ES5 equivalent of the below code?下面代码的ES5等价物是什么?

Object.keys($location.search()).forEach( key => ($location.search()[key] == null || $location.search()[key] == "") && $location.search([key], null));

I tried to replace the => with just a simple = , that resulted in a error however.我试图用一个简单的=替换=> ,但这导致了错误。

How do I convert the above code to the ES5 syntax.如何将上述代码转换为ES5语法。

You can either convert it into a function yourself or use something like Babel to do it for you您可以自己将其转换为function也可以使用Babel之类的工具为您完成

Object.keys($location.search()).forEach(function (key) {
    return ($location.search()[key] == null || $location.search()[key] == "") && $location.search([key], null)
});

To replace a arrow function you can´t just use a =, you need to use a regular function statement like:要替换箭头函数,您不能只使用 =,您需要使用常规函数语句,例如:

(param) => {
  //code block 
}

function(param) {
  //code block
}

Just use an anonymous function (see documentation on forEach ):只需使用匿名函数(请参阅有关forEach文档):

Object.keys($location.search()).forEach(function (key) {
    return ($location.search()[key] == null || $location.search()[key] == "")
            && $location.search([key], null);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM