Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果得到的行号有错误,为什么建议使用命名函数进行调试?_Javascript - Fatal编程技术网

Javascript 如果得到的行号有错误,为什么建议使用命名函数进行调试?

Javascript 如果得到的行号有错误,为什么建议使用命名函数进行调试?,javascript,Javascript,我正在经历这一切 并看到这项建议: 即使函数已命名,也可以使用变量名 调用它。在调试代码时,命名它将很有帮助。但是它 不会影响我们调用它的方式 因此,我使用节点运行它,但将其修改为: //匿名函数 const foo=函数(){ 控制台日志(“foo”); console.log(axe);//添加了创建错误的行 } foo();//用括号调用函数 并得到了错误信息: /Users/user/Documents/Practice/JavaScript/MDN/first_class_funct

我正在经历这一切

并看到这项建议:

即使函数已命名,也可以使用变量名 调用它。在调试代码时,命名它将很有帮助。但是它 不会影响我们调用它的方式

因此,我使用
节点运行它,但将其修改为:

//匿名函数
const foo=函数(){
控制台日志(“foo”);
console.log(axe);//添加了创建错误的行
}
foo();//用括号调用函数
并得到了错误信息:

/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:8
控制台日志(axe);
^
ReferenceError:未定义axe
在foo(/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:8:17)
反对。(/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:10:1)
at模块编译(内部/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1167:10)
在Module.load(内部/modules/cjs/loader.js:996:32)
at Function.Module._load(内部/modules/cjs/loader.js:896:14)
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:71:12)
在internal/main/run_main_module.js:17:47
现在使用命名函数
logBar()

我没有得到任何额外的信息:

/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:14
    console.log(axe);
                ^

ReferenceError: axe is not defined
    at logBar (/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:14:17)
    at Object.<anonymous> (/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:14
控制台日志(axe);
^
ReferenceError:未定义axe
在日志栏(/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:14:17)
反对。(/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:16:1)
at模块编译(内部/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1167:10)
在Module.load(内部/modules/cjs/loader.js:996:32)
at Function.Module._load(内部/modules/cjs/loader.js:896:14)
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:71:12)
在internal/main/run_main_module.js:17:47

由于node告诉我问题发生在哪一行,这难道不排除使用命名函数进行调试的原因吗?除非通过调试,否则它们意味着如果存在复杂的业务逻辑,则不需要所有上下文和通过代码解析就可以找到函数的用途。MDN的作者还提到了哪些调试问题

MDN文档可能是在ES2015之前编写的,它为许多常见情况添加了函数名推断

但匿名函数可以在不与名称关联的情况下使用:

setTimeout(函数(){
控制台日志(axe);

}, 1000);MDN文档可能是在ES2015之前编写的,它为许多常见情况添加了函数名推断

但匿名函数可以在不与名称关联的情况下使用:

setTimeout(函数(){
控制台日志(axe);

}, 1000);如果函数是匿名的,并且无法推断函数名,则会有所帮助,例如,对于回调函数:
const callSomeFunction=(func)=>func();callSomeFunction(function(){throw new Error();})()。stacktrace只会将其标记为
。在什么情况下不能推断出函数名(ES2015之前的情况除外)?如果函数是匿名的且不能推断出函数名,则会有所帮助,例如,对于回调函数:
const callSomeFunction=(func)=>func();callSomeFunction(function(){throw new Error();})()。stacktrace会将其标记为
。在什么情况下,除了pre-ES2015之外,不能推断函数名?那么,在comment to question中使用由用户4642212链接的pre-ES2015 JS?那么在comment to question中使用由用户4642212链接的pre-ES2015 JS?
const bar = function logBar() {
    console.log("bar");
    console.log(axe);
}
bar();
/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:14
    console.log(axe);
                ^

ReferenceError: axe is not defined
    at logBar (/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:14:17)
    at Object.<anonymous> (/Users/user/Documents/Practice/JavaScript/MDN/first_class_function.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47