美文网首页
6.JS函数操作

6.JS函数操作

作者: 若愚同学 | 来源:发表于2018-06-12 22:06 被阅读0次
//1.有名字的函数
function name1(){//无参数,无返回值
    console.log("method is name1");
}

function name2(username){//有参数无返回值
    console.log("welcome:["+username+"]");
}

function name3(a,b){//有参数有返回值
    //如果只传一个值,那么将会返回NaN
    return a+b;//才js中如果需要返回值,直接return就可以了
}

name1();

name2("小林");

var ret = name3(1,3);
console.log(ret);

//2.没有名字的函数
/*
 * 语法
 * var method = function([形参1,形参2...]){
 *                      //函数体,代码逻辑
 *              }
 */
var method = function(){
    console.log("你好世界");
    return "意思一下";
};

//调用
method();

//将变量method赋给method1
var method1 = method;
method1();

//注意:此时只是接收method的返回值,而不是将method本身赋值给method2
var method2 = method();
console.log(method2);

















相关文章

网友评论

      本文标题:6.JS函数操作

      本文链接:https://www.haomeiwen.com/subject/xitdeftx.html