返回值的类型(源码见20200809----02.html)
- break,continue,return的区别?
break是退出当前的循环
continue是跳出当次循环
return是结束整个函数(不论循环内外,return后的代码都不执行)
function fun(){
alert("函数开始执行");
for(var n=0; n<5; n++){
if(n==2){
//break;
//continue;
return;
}
console.log(n);
}
alert("函数执行完了")
}
fun();
2.返回值可以是任意的数据类型
可以是一个对象,也可以是一个函数
function fun2(){
return {name:"沙和尚"}
}
var a = fun2()
console.log(a)
console.log(a.name)
function fun3(){
function fun4(){
console.log("我是fun4")
}
fun4()
}
fun3()
将fun4函数对象作为返回值返回:
function fun3(){
function fun4(){
console.log("我是fun4")
}
return fun4
}
var a = fun3()
console.log(a)
function fun3(){
function fun4(){
console.log("我是fun4")
}
return fun4()
}
var a = fun3()
console.log(a)
fun4(),调用函数,输出“我是fun4”
return fun4(),因为console.log(“我是fun4”)的返回值为undefined,所以为return undefined,最后输出undefined