js 传递方法

js 中可以把函数(方法)当做参数传递:

Js代码   收藏代码
  1. <script type="text/javascript">  
  2.        var A= function (args2) {  
  3.            console.log('A start....');  
  4.            console.log('argument:\t'+args2);  
  5.            console.log('A end.');  
  6.        };  
  7.        var B= function (fn2, context) {  
  8.            console.log('B start,,,,');  
  9.            var arg22='参数';  
  10.            fn2.call(context,arg22);  
  11.            console.log('B end,');  
  12.        };  
  13.    </script>  

 上面时函数声明,下面是函数调用:

Js代码   收藏代码
  1. <script type="text/javascript">  
  2.     B(A);  
  3. </script>  

 把函数A 作为参数传递给B.

执行结果:

 

执行序列如下:

 

还有一种方式:

Js代码   收藏代码
  1. var A= function (args2) {  
  2.             console.log('A start....');  
  3.             console.log('argument:\t'+args2);  
  4.             console.log('A end.');  
  5.         };  
  6.         var B= function (fn2, context) {  
  7.             console.log('B start,,,,');  
  8.             var arg22='参数';  
  9.             fn2(arg22);  
  10.             console.log('B end,');  
  11.         };  

 执行结果相同.

 

类似的函数调用函数的例子:

Js代码   收藏代码
  1. if (evtType == 'focus' && settings.focus_callback && typeof settings.focus_callback === 'function') {  
  2.                     e = e || window.event || arguments.callee.caller.arguments[0];  
  3.                     settings.focus_callback(e);  
  4.                 }  

 

 

那么Java中可以把函数当做参数传递吗?

不行!!!

那么java中可以当做参数传递的有哪些呢?

(1)数据类型,比如int,String,List

(2)自定义对象,比如Student,Person,School等

 

那么有时候真的需要把方法当做参数传递,例如:

(1)监听界面用户操作,比如监听用户点击事件,需要给这个监听器传递一个回调方法X,即监听到用户点击之后就执行方法X

(2)代理,比如有个代理,需要传入两个函数,分别在代理实际方法前后执行

 

Java代码   收藏代码
  1. public class Main {  
  2.     public void B(A a){  
  3.         System.out.println("B start,,,,");  
  4.         String arg22="参数";  
  5.         a.callback(arg22);  
  6.         System.out.println("B end,");  
  7.     }  
  8.       
  9.     public static void main(String[] args) {  
  10.         new Main().B(new A());  
  11.           
  12.     }  
  13. }  
  14.   
  15. class A{  
  16.     public void callback(String args2){  
  17.         System.out.println("A start....");  
  18.         System.out.println("argument:\t"+args2);  
  19.         System.out.println("A end.");  
  20.     }  
  21. }  

 

相当于方法B 调用了方法callback.

进一步优化:

Java代码   收藏代码
  1. package com;  
  2.   
  3. public class Main {  
  4.     public void B(A a){  
  5.         System.out.println("B start,,,,");  
  6.         String arg22="参数";  
  7.         a.callback(arg22);  
  8.         System.out.println("B end,");  
  9.     }  
  10.       
  11.     public static void main(String[] args) {  
  12.         new Main().B(new A(){  
  13.             public void callback(String args2){  
  14.                 System.out.println("A start....");  
  15.                 System.out.println("argument:\t"+args2);  
  16.                 System.out.println("A end.");  
  17.             }  
  18.         });  
  19.           
  20.     }  
  21. }  
  22.   
  23. interface A{  
  24.     public void callback(String args2);  
  25. }  

 是不是很像事件监听器呢?

Java代码   收藏代码
  1. qrComboBox.addItemListener(new ItemListener()  
  2.         {  
  3.             @Override  
  4.             public void itemStateChanged(ItemEvent arg0)  
  5.             {  
  6.                 String  selectedPic=(String)qrComboBox.getSelectedItem();  
  7.                 if(!ValueWidget.isNullOrEmpty(selectedPic)){  
  8.                     inputQRTextArea.setText(selectedPic);  
  9.                     generateQRAction(false);  
  10.                     System.out.println("addItemListener");  
  11.                 }  
  12.             }  
  13.         });  

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值