函数声明
function foo () {}
函数表达式
var foo = function () {}
(function() {})()
var foo = function bar() {}
return function () {}
函数声明会被提前
var sum = add(1,2); // 3 console.log(sum); function add(x, y) { x = +x; y = +y; if(isNaN(x) || isNaN(y)) { return; } return x+y; }
变量会被提前, 而函数表达式不会提前
var sum = add(1,2); //Uncaught TypeError: add is not a function console.log(sum); var add = function (x, y) { x = +x; y = +y; if(isNaN(x) || isNaN(y)) { return; } return x+y; }
时间: 2024-09-30 06:54:53