js 函数的方法 函数调用模式 this => that arguments

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <script type="text/javascript">
        //方法调用
        var myObject = {
            values: 0,
            increment: function (inc) {

                this.values += typeof inc === "number" ? inc : 1;
            }


        }
        myObject.increment();
        document.writeln(myObject.values);// 1   
        myObject.increment(2);
        document.writeln(myObject.values);// 3  理解同一个对象
        //函数调用
        var add = function (a, b) {

            return a + b;
        }
        var sum = add(3, 4);

        myObject.double = function () {
            var that = this;
            var helper = function () {
                that.values = add(that.values, that.values);

            }
            helper();

        }
        myObject.double();
        document.writeln(myObject.values);// 6 
        // arguments 免费赠送的参数   如果参数过少的话 ,缺失的值 是 undefined 
        var sum = function () {
            var sum = 0;
            for (var i = 0; i < arguments.length; i++) {

                sum += arguments[i];
            }
            return sum;
        }
        document.write(sum(10, 20, 30));

    </script>
</body>
</html>

 

posted @ 2015-10-22 20:16  刀小爱  阅读(188)  评论(0编辑  收藏  举报