dojo amd 初始化方法自动运行

dojo amd init method autorun

我有一个 js 看起来像:

    abinit();
    function abinit(){}
    function hello{var a=12; return a;}

    window.requestAnimFrame = (function(){
      return  window.requestAnimationFrame || 
              window.webkitRequestAnimationFrame || 
              window.mozRequestAnimationFrame || 
              window.oRequestAnimationFrame || 
              window.msRequestAnimationFrame || 
        function(/* function FrameRequestCallback // callback,// DOMElement Element  element){
        window.setTimeout(callback, 1000 / 60);
      };
     })();

AMD 转换后的代码如下所示

 define(["dojo/ready","dojo/dom","dojo/dom-construct","dojo/_base /fx","dojo/fx","dojo/dom-style","dojo/parser","dojo/window", "dojo/dom-attr","dojo/domReady!"],  
     function(ready,dom,domConstruct,baseFx,coreFx,domStyle,parser,win,domAttr,) {
       var abGlobal = this;
       abGlobal.abStatus = false;
       return{
            abInit:function() { ...... },
            hellow:function(){var a=12; return a;}
      }
});

几乎没有问题

  1. 转成dojo amd时如何调用init方法?

  2. 如何根据dojo转换requestAnimFrame?

  3. AMD 的正确方法是什么(return 或 var ={function abInit()} 方法中的方法?

您可以尝试使用 declare return 一个 "Class" 并在 constructor.

中调用您的方法

此处示例:

define([
        "dojo/ready",
        "dojo/dom",
        "dojo/dom-construct",
        "dojo/_base /fx",
        "dojo/fx",
        "dojo/dom-style",
        "dojo/parser",
        "dojo/window",
        "dojo/dom-attr",
        "dojo/_base/declare",
        "dojo/domReady!"
      ],
      function(ready, dom, domConstruct, baseFx, coreFx, domStyle, parser, win, domAttr, ) {
        var abGlobal = this;
        abGlobal.abStatus = false;
        return declare(null,{
          constructor:function(){
            this.abInit(); // call your init here
          },
          abInit: function() {
          },
          hellow: function() {
            var a = 12;
            return a;
          }
        });
      });

或者 return 和对象之后,在需要你的模块之后调用你的方法,例如:

define([
    "dojo/ready",
    "dojo/dom",
    "dojo/dom-construct",
    "dojo/_base /fx",
    "dojo/fx",
    "dojo/dom-style",
    "dojo/parser",
    "dojo/window",
    "dojo/dom-attr",
    "dojo/_base/declare",
    "dojo/domReady!"
  ],
  function(ready, dom, domConstruct, baseFx, coreFx, domStyle, parser, win, domAttr, ) {
    var abGlobal = this;
    abGlobal.abStatus = false;
    // return an object here
    return { 
      abInit: function() {
      },
      hellow: function() {
        var a = 12;
        return a;
      }
    };
  });

  require(['yourModule'],function(yourModule){
    yourModule.abInit(); // call your method here

  });