dojo框架用hitch实现函数与上下文的绑定

前端之家收集整理的这篇文章主要介绍了dojo框架用hitch实现函数与上下文的绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

dojo框架:http://dojotoolkit.org/


  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <Metacharset="UTF-8">
  5. <title>dojo框架绑定函数与上下文</title>
  6. <scriptsrc="dojo/dojo/dojo.js"></script>
  7. </head>
  8. <body>
  9.  
  10. <script>
  11.  
  12. require(["dojo/_base/lang"],function(lang){
  13. vartheAccumulator={
  14. total:0,clear:function(){
  15. },clear:function(){
  16. this.total=0;
  17. },add:function(x){
  18. this.total+=x;
  19. },getResult:function(){
  20. returnthis.total;
  21. }
  22. };
  23. theAccumulator.clear();
  24. theAccumulator.add(1);
  25. theAccumulator.add(2);
  26.  
  27. functionprintResult(func){
  28. console.log(func());
  29. }
  30. printResult(lang.hitch(theAccumulator,"getResult"));//输出3
  31. });
  32.  
  33.  
  34. </script>
  35. </body>
  36. </html>

猜你在找的Dojo相关文章