Blogger Information
Blog 32
fans 2
comment 2
visits 23315
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS作业0226】数组声明访问遍历与函数调用
暴风战斧
Original
670 people have browsed it

【数组的遍历】

// 定义数组
var arr = ['apple', 'orange', 'cherry', 'pear'];
arr['username'] = 'jack';

// 遍历数组
for (var i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}

// 遍历对象
for (var i in arr) {
    console.log(arr[i]);
}

11.png

【函数调用与参数处理】

<script>
    // 1、函数的调用
    function add(a, b) {
        return a + b;
    }
    // 赋值
    var sum = add;
    console.log(sum(10, 30));

    // 对象属性
    var obj = {};
    obj.sum = add;
    console.log(obj.sum(20, 10));

    // 函数当参数:回调
    function func1(callback, a, b) {
        return callback(a, b);
    }
    console.log(func1(add, 11, 34));

    // 函数当返回值
    function func2() {
        return add;
    }
    console.log(func2()(33, 21));

    // 函数也是对象, 对象就会有属性/方法
    // 函数有name, length
    console.log(add.name);
    console.log(add.length);

    // 函数也有方法: toString(), 返回函数源代码
    console.log(add.toString());


    // 2、函数参数的处理
    // arguments方法,基本不用了
    function sum1(a, b, c) {
        console.log(arguments);
        for (var i = 0; i < arguments.length; i++) {
            console.log(arguments[i]);
        }
        console.log(a + b);
    }
    sum1(1,2,3,4,5);

    // ES6:剩余参数
    function sum2(...params) {
        console.log(params);
    }
    sum2(1,2,3,4,5);

</script>

22.png

【控制台运行效果】

1.png2.png3.png

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:博客已经可以发代码了, 不必截图也行
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments