JavaScript函数学习

//1.命名函数表达式 
var test = function abc() {
    console.log(1);
}

test(); // 执行 test函数
abc(); // abc is not defined; 表达式的函数名不能直接使用


//2.匿名函数表达式 
// 常用的方式,函数表达式一般都是指匿名函数表达式
var demo = function() {
    console.log(2);
}

demo(); // 执行demo函数

// 传参 a b 形参 只是个变量,形式参数 c 未传值,为 undefined
function add(a, b, c) {
    var c = a + b;
    console.log(c);
}
add(1,2);//输出3 这里的 1,2是实际参数

// 任意数字求和
function sum() {
// 类数组 arguments = [1,2,3]
    console.log(arguments.length)
    var res = 0;
    for(var i = 0; i < arguments.length; i++) {
        res +=i;    
    }
}
sun(1,2,3);

// arguments 
// 当形参的值和arguments里面的值一样时候,改变arguments的值时 也对应改变了形参的值
function arg(a, b) {
    arguments[0] = 1;
    console.log(a) // 1;
    arguments[1] = 2;
    console.log(b); // undefined;
}
arg(6);


// return 函数返回值
function myNumber(target) {
    return +target;
}
myNumber('123');

猜你喜欢

转载自blog.csdn.net/fanlixing6/article/details/85041892