zoukankan      html  css  js  c++  java
  • js09--函数 call apply

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Untitled Document</title>
            <script type=text/javascript charset=utf-8>
            var k = 10 ; 
            function test(){
                this.k = 20;
            }
            alert(window.k);//10
            alert(k);//10
            test();//window.test()
            alert(test.k);// undefined
            alert(window.k);//20
            alert(k);//20
            
            var t = new test();
            alert(t.k);//20
            // this:this对象是指在运行时期基于执行环境所绑定的
            // this总是指向调用者,也就是说 谁调用了我 我就指向谁
            </script>
        </head>
        <body>
        </body>
    </html>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Untitled Document</title>
            <script type=text/javascript charset=utf-8>
            function sum(x , y){
                return x+y;
            }
            function call1(num1 , num2){
                return sum(num1 , num2);//函数可以嵌套调用
            }
            console.log(call1(1,2));
    /* 每一个函数都包含两个非继承而来的方法:call、apply。把方法动态加在对象上,给对象加一个已有的新方法,代码的复用。语句执行完对象就没有这个方法了。
    使用call()、aplly()来扩充作用域的最大好处就是对象不需要与方法有任何耦合关系。 */
            //call apply  把方法动态加在对象上,给对象加一个已有的新方法,代码的复用。简单的用法:绑定一些函数 用于传递参数 调用 
            function sum(x , y){
                return x+y;
            }
            function call1(num1 , num2){
                return sum.call(this , num1 , num2);//调用的方法-call-(对象,参数)
            }
            function apply1(num1 , num2){
                return sum.apply(this , [num1,num2]);
            }
            alert(call1(10 , 20));
            alert(apply1(20,40));
    
            window.color = 'red';
            var obj = {color:'blue'};
            var obj2 = {color:'yellow'};
            function showColor(){
                alert(this.color);
            }
            showColor.call(window);
            showColor.call(obj);//把方法加在对象上,给对象加一个已有的新方法。
            
            // call方法的简单模拟与实现
            //function 方法
            function test1(a , b){
                return a+b;
            }
            // 函数名字大写,表示是类,(规范)
            function Obj(x, y){
                this.x = x ; 
                this.y = y ;
                return x*y;
            }
            var o = new Obj(10 , 20);//return对o没影响
            o.method = test1 ;
            alert(o.method(o.x , o.y));
            delete o.method;//o不是{}格式,也可以删除方法属性,语句执行完对象就没有这个方法了
            
            alert(test1.call(o,o.x ,o.y));//语句执行完对象就没有这个方法了
            
            ===============================================================
            function Obj(x, y){
                this.x = x ; 
                this.y = y ;
                this.say = function(){alert(123);}
                return x*y;
            }
            function test1(a , b){
                return a+b;
            }
            var o = new Obj(10 , 20);//return对o没影响
    
            alert(o.x);
            delete o.x;
            alert(o.x);//undefined
    
            o.say();//123
            delete o.say;
            o.say();//o.say is not a function
    
            o.method = test1 ;
            alert(o.method(o.x , o.y));//30
            delete o.method;//语句执行完对象就没有这个方法了
            alert(o.method(o.x , o.y));//o.method is not a function
            </script>
        </head>
        <body>
        </body>
    </html>
  • 相关阅读:
    【阿里云产品公测】云引擎ACE新手实战基于Wordpress
    【阿里云产品公测】结构化数据服务OTS之JavaSDK初体验
    洗牌算法详解
    常用的位操作
    字符串乘法
    如何运用二分查找算法
    递归详解
    判断回文链表
    子集、排列、组合问题汇总
    接雨水问题详解
  • 原文地址:https://www.cnblogs.com/yaowen/p/6864527.html
Copyright © 2011-2022 走看看