JavaScript之call方法学习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Call Function Study</title>

    <script type="text/javascript" charset="utf-8">

        function sum(a,b) {
            return a+b;
        }


        function call1(a,b) {
            return sum.call(this,a,b);
        }


        function apply1(a,b) {
            return sum.apply(this,[a,b]);
        }

        window.color = 'red';

        var obj1 = {color:'blue'};

        function showColor() {
            document.write(this.color);
            document.write("<br>");
            document.write("<br>");
        }

        document.write(call1(10,20));
        document.write("<br>");
        document.write(apply1(100,20));
        document.write("<br>");
        document.write("<br>");
        document.write("<br>");

        showColor.call(this);
        showColor.call(window);
        showColor.call(obj1);

        document.write("<br>");
        document.write("<br>");
        document.write("<br>");

        
        
        //模拟call方法
        function test1(a,b,c) {
            return a+b+c;
        }

        function Obj1(a,b,c) {
            this.a = a;
            this.b = b;
            this.c = c;
            return a-b-c;
        }

        var o = new  Obj1(2,2,3);

        document.write(o + ' type: ' + (typeof  o) + ' ' + o.a);
        document.write("<br>");
        document.write("<br>");
        document.write("<br>");
        document.write(test1.call(o, o.a, o.b, o.c));


        o.method = test1;
        document.write("<br>");
        document.write("<br>");
        document.write("<br>");
        document.write(o.method(o.a, o.b, o.c));
        delete  o.method;



    </script>


</head>
<body>

</body>
</html>




学习资料: http://www.icoolxue.com/play/9358 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值