zoukankan      html  css  js  c++  java
  • JavaScript: The Good Parts 学习随笔(五)

    4.4 参数

    当函数被调用时,会得到一个参数列表,即arguments数组(并不是真的数组,只有length属性,不含方法)。

    // Make a function that adds a lot of stuff.
    // Note that defining the variable sum inside of
    // the function does not interfere with the sum
    // defined outside of the function. The function
    // only sees the inner one.
    
    var sum = function (  ) {
        var i, sum = 0;
        for (i = 0; i < arguments.length; i += 1) {
            sum += arguments[i];
        }
        return sum;
    };
    
    document.writeln(sum(4, 8, 15, 16, 23, 42)); // 108

    4.5 返回

    一个函数总会返回一个值,没有指定就是undefined。

    如果函数调用时在前面加上一盒new,且返回的不是一个对象,则返回this,即该新对象。

    4.6 异常

    var add = function (a, b) {
        if (typeof a !== 'number' || typeof b !== 'number') {
            throw {
                name: 'TypeError',
                message: 'add needs numbers'
            }
        }
        return a + b;
    }
    // Make a try_it function that calls the new add
    // function incorrectly.
    
    var try_it = function (  ) {
        try {
            add("seven");
        } catch (e) {
            document.writeln(e.name + ': ' + e.message);
        }
    }
    
    tryIt(  );
  • 相关阅读:
    可持久化BCJ
    Codeforces 911 三循环数覆盖问题 逆序对数结论题 栈操作模拟
    找不同
    最接近的三数之和
    找到所有数组中消失的数字
    三数之和
    小程序中的变量
    二叉树的最近公共祖先
    深拷贝和浅拷贝
    下载安装JDK
  • 原文地址:https://www.cnblogs.com/ltchronus/p/2862802.html
Copyright © 2011-2022 走看看