Nodejs基础系列-04- javascript 函数

//01- function(){}
function myFunction() {
    console.log("hello world!");
}
myFunction();

//02-传递变量给函数
function greeting (name,city) {
    console.log("Hello "+ name);
    console.log(". HOW is the weather in "+ city);

}
let name="Alex";
greeting(name,"Beijing");


//03-函数被付给了haha变量;支持函数编程,目前java还不支持,python也支持的。
let haha=function varing() {
    console.log("haha runing!")
}
haha();
//haha runing!

//04-return  从函数返回值,可以包含多个retrun;一旦触发就返回,函数的代码不在执行,不会继续执行后面的。
function formatGreeting(name,city) {
   var retStr="";
   retStr+="Hello :" + name ;
   retStr+=", Wellcome to "+ city + "!";
   return retStr;
}
var greeting=formatGreeting("Tom","Beijing");
console.log(greeting);
//Hello :Tom, Wellcome to Beijing!


//05-匿名函数,当你调用其他函数时,可以在参数组中直接定义他们(不指定函数名))
function doCalc(num1,num2,calcFunction){
       console.log(calcFunction(num1,num2));
        return calcFunction(num1,num2);
}
doCalc(5,20,function (x,y){return x+y;});//25
//doCalc(5,20,function (x,y){return x*y;}); //100
//doCalc(5,20,(x,y)=>{return  x+y; }); //25

//06-箭头函数;相当于剔除function关键字和函数名
()=>{
  console.log("hello me");
}

//箭头函数自动执行
((name)=>{
    console.log("hello :" + name);

})("Alice");

发布了40 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/LUCKWXF/article/details/104127542