1:声明函数
function put(something
){
console
.log("1" + something
+ "=");
}
function add(a
,b
){
return a
+ b
;
}
function testNum(num
){
if(num
< 0 ) return;
return num
> 10;
}
2:调用函数
function put(something
){
console
.log("1" + something
+ "=");
}
put(2);
function add(a
,b
){
return a
+ b
;
}
add(1,2);
console
.log(add(1,2));
var result
= add(1,2)
console
.log(result
);
console
.log(add(result
,5));
function testNum(num
){
if(num
< 0 ) return;
return num
> 10;
}
console
.log(testNum(-5));
console
.log(testNum(15));
3:函数表达式
function add(a
,b
){
return a
+ b
;
}
console
.log(add
);
var plus
= add
;
var res
= plus(5,6);
console
.log(res
);
var multiply = function a(可以省略不写
) (a
,b
){
return a
* b
;
}
console
.log(multiply(2,3));
4:变量和函数的提升(Hoisting)
x
= 5;
console
.log(x
);
var x
;
console
.log(divide(8,4));
function divide(a
,b
){
return a
/ b
}
5:默认参数
function greetings(name
= "z
){
console
.log("你好," + name
);
}
greetings();
greetings("zhou");
function greetingsWithWeather(name
= "z", weather
){
console
.log("你好," + name
+ ",今天是"+weather
);
}
greetingsWithWeather(undefined
,"晴天");
6:递归:函数自己调用自己
function sum(n
){
if(n
=== 1){
return 1;
}
return n
= sum (n
-1);
}
console
.log(sum(100));
function fib(num
){
if(num
<= 1){
return 1;
}return fib(num
-1)+fib(num
-2);
}
console
.log(fib(5));
7:arguments函数里内置的跟数组类似的参数集合
function log(){
console
.log(arguments
[0]);
}
log("abc");
function logs(){
console
.log(arguments
[0]);
}
logs("abc","bcd");
function loges(){
if(let i
=0,i
<arguments
.length
;i
++){
console
.log(arguments
[0]);
}
}
loges("abc","bcd","efg");
8:作用域,指自定义变量的可以范围,分局部作用域(函数内部定义的变量),全局作用域(在最外层定义的变量)
var x
= 5
function add(a
){
var y
= 10;
console
.log(y
);
return a
+ x
;
}
console
.log(add(8));
console
.log(y
);
var num
= 100;
function multiply(num
){
return num
* 10;
}
console
.log(multiply(4));
10:var,let(const定义的常量跟let定义的变量一样)
相同之处:
1)都是用来定义变量的;
2)在函数内部定义的变量,出了函数,就不能再访问;
3)定义的全局变量在哪里都能访问;
不同之处:
1)除了函数之外,在其他代码块内部定义的变量,出了函数是否能访问
var z
= 6;
if(z
> 2){
console
.log(z
);
var innerA
= 17;
let innerB
= 17;
}
console
.log(innerA
);
console
.log(innerB
);
for (var i
= 0;i
< 10;i
++){
console
.log(i
);
}
console
.log(i
);
for (let i
= 0;i
< 10;i
++){
console
.log(i
);
}
console
.log(i
);
11:箭头函数
var greeting = () =>{
console
.log("hello");
}
greeting();
var greeting = name
=>{
console
.log("hello"+name
);
}
greeting("z");
var greetings = (name
,age
) =>{
console
.log("hello"+name
+19);
}
greetings("z",19);
var num = x
=> x
+1;
console
.log(num(2));
12:闭包(函数内部再定义函数)
function squareSum(a
,b
){
function square(x
){
return x
* x
;
}
return square(a
) + square(b
)
}
console
.log(2,3);
function person(){
let name
= "z";
function getName(){
return name
;
}
return getName
;
}
var getName
= person();
console
.log(getName
);
console
.log(getName());
13:柯里化(curry:把一个接收多个参数的函数,变成一系列接收一个参数的内部函数)
function addThreeNums(a
,b
,c
){
return a
+ b
+ c
;
}
console
.log(addThreeNums(1,2,3));
function addThreeNumsCurry(a
){
return function(b
){
return function(c
){
return a
+ b
+ c
;
};
};
}
console
.log(addThreeNumsCurry(1)(2)(3));
var fixedTwo
= addThreeNumsCurry(1)(2);
console
.log(fixedTwo(4));
console
.log(fixedTwo(5));
console
.log(fixedTwo(6));
14:自执行函数(函数在定义完后,直接调用它自己)
自执行内部的代码,外部访问不到,防止被篡改,内部也形成了自己的作用域,防止和外部的变量命名冲突
var num1
= 10;
(function(){
var num1
= 20;
console
.log(num1
);
})();
console
.log(num1
);
15:回调函数
function request(cb
){
console
.log("请求数据");
cd("success");
console
.log("请求结束");
}
function callback(result
){
console
.log("执行回调");
console
.log("执行结果是;"+ result
)
}
request(callback
);