简体   繁体   English

es6 箭头函数调试器语句

[英]es6 arrow functions debugger statement

If I have a function like that:如果我有这样的 function:

param => params + 1

and I need to put a debugger statement inside the function's body.我需要在函数体内放置一个debugger语句。 Is adding parenthesis like this:像这样添加括号:

param => { debugger; return params + 1 }

the only option?唯一的选择?

From the MDN article on arrow functions: 来自MDN关于箭头功能的文章:

 (param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression 

You can see that the brace-less syntax requires the code on the right of the arrow to be an expression , which is an (unfortunate) distinction made by the language itself. 您可以看到无括号语法要求箭头右侧的代码是表达式 ,这是语言本身所做的(不幸) 区分

Since debugger is a statement, using it anywhere an expression is expected is a syntax error. 由于debugger是一个语句,因此在预期表达式的任何地方使用它都是语法错误。 One thing you could to to work around this is to transform your debugger statement in an expression which you trick JavaScript into evaluating but not returning, eg: 你可以解决这个问题的一个方法是在一个表达式中转换你的调试器语句,你可以用JavaScript来评估但不返回,例如:

function debug(args) {
     debugger;
     return true;
}

params => debug() && params + 1

// or

params => console.log(params) || params + 1

The way this works is that because of the way logical operators function in JavaScript, this is true: 这种方式的工作原理是,由于逻辑运算符在JavaScript中的运行方式,这是正确的:

truthyA && B  === B

falsyA || B === B

When chaining logical operators, JavaScript evaluates sub-expressions left to right and then act depending on their boolean equivalent. 当链接逻辑运算符时,JavaScript从左到右计算子表达式,然后根据它们的布尔等价物进行操作。 That's why you'll sometimes see && used in place of if statements: 这就是为什么你有时会看到&&代替if语句:

 if (smth) doStuff();
 // is equivalent to:
 smth && doStuff();

Use console.log with a logical OR operator. 将console.log与逻辑OR运算符一起使用。 This writes the arguments to the console. 这会将参数写入控制台。

const add = (x, y) => console.log(x, y) || x + y;

This is another way to do it, which doesn't require defining a function: 这是另一种方法,它不需要定义函数:

(() => { debugger; })()

It is a self invoking arrow function that calls the debugger statement, and it is also an expression. 它是一个自调用箭头函数,它调用debugger语句,它也是一个表达式。

So in your code you could use: 所以在你的代码中你可以使用:

param => (() => { debugger; })() && params + 1

It's a bit harder to type, but does the trick anywhere an expression is expected. 输入有点困难,但是在表达式预期的任何地方都可以使用。

Note that if you want params + 1 to also be executed (eg continue after debug statement) you have to return true from the function: 请注意,如果您还希望执行params + 1 (例如,在调试语句之后继续),则必须从函数返回true:

(() => { debugger; return true })()

have not found decent fat arrow function inline debugger injection yet.尚未找到合适的胖箭头 function 内联调试器注入。 Option:选项:

 ()=>console.log(9)?? (()=>{debugger} )() ?? console.log(0) 

console is a stub of any code.控制台是任何代码的存根。 (()=>{debugger} )() seems only way to make debugger inline. (()=>{debugger} )()似乎是使调试器内联的唯一方法。 In your case:在你的情况下:

param => (()=>{debugger} )() ?? params + 1

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

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