

新闻资讯
技术学院用 function 关键字声明命名函数:function 函数名(参数) { 函数体 },函数被提升、可提前调用,参数不匹配时少传为 undefined、多传被忽略,箭头函数无独立 this 且不可构造。
函数是 JavaScript 中可重复执行的代码块,它封装逻辑、接收输入(参数)、返回结果(可选),本质是一个值,可以赋值、传参、甚至作为返回值。
function 关键字声明一个命名函数?这是最基础也最兼容的声明方式,函数会被提升(hoisted),可以在声明前调用。
calculateTotal)return 后的值为返回值;无 return 或只写 return; 时返回 undefined
function greet(name) {
return 'Hello, ' + name + '!';
}
console.log(greet('Alice')); // 输出:Hello, Alice!调用就是加括号执行,无论有没有参数。JavaScript 不校验参数个数,多传或少传都不会报错,但可能引发逻辑错误。
undefined
arguments 或剩余参数 ...args)function add(a, b) {
return a + b;
}
console.log(add(2)); // NaN(因为 b 是 undefined,2 + undefined → NaN)
console.log(add(2, 3, 5)); // 5(第三个参数 5 被忽略)() => {} 和普通函数有什么关键区别?箭头函数是 ES6 引入的简洁写法,但它不是语法糖——它没有自己的 this、arguments、super 或 new.target,也不能用作构造函数。
this 绑定:它的 this 指向外层作用域的 this,适合回调场景(如事件处理、map)return
call/apply/bind 改变 this
prototype 属性,typeof 仍是 "function"
const square = x => x * x; console.log(square(4)); // 16const obj = { value: 10, regular: function() { return this.value; }, arrow: () => this.value }; console.log(obj.regular()); // 10 console.log(obj.arrow()); // undefined(箭头函数中 this 指向全局或 undefined)
真正容易被忽略的是:函数名在错误堆栈、调试器和递归调用中起关键作用。匿名函数(包括多数箭头函数)在堆栈里
显示为 anonymous,排查问题成本更高;而命名函数能清晰暴露调用链。别为了省几个字符放弃可维护性。