dojo amd init方法自动运行

时间:2016-01-22 05:15:03

标签: javascript dojo initialization

我有一个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的正确方法是什么(返回方法或var = {function abInit()}方式?

1 个答案:

答案 0 :(得分:1)

您可以尝试返回&#34; Class&#34;使用@Warmup(iterations = 10, time = 700, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 700, timeUnit = TimeUnit.MILLISECONDS) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Fork(5) public class StreamTest { @State(Scope.Benchmark) public static class LengthContainer { @Param({ "100", "1000", "3000" }) private int length; } private static double[][] toArrayStream(int rowDimension, int columnDimension) { return IntStream.range(0, Math.min(rowDimension, columnDimension)).mapToObj(r -> { double[] row = new double[columnDimension]; row[r] = 1; return row; }).toArray(double[][]::new); } private static double[][] toArrayStreamForEach(int row, int column) { double[][] identity = new double[row][column]; IntStream.range(0, row).forEach(i -> identity[i][i] = 1); return identity; } private static double[][] toArrayForLoop(int row, int column) { double[][] matrix = new double[row][column]; for (int i = 0; i < Math.min(row, column); i++) { matrix[i][i] = 1; } return matrix; } @Benchmark public double[][] toArrayStream(LengthContainer container) { return toArrayStream(container.length, container.length); } @Benchmark public double[][] toArrayForLoop(LengthContainer container) { return toArrayForLoop(container.length, container.length); } @Benchmark public double[][] toArrayStreamForEach(LengthContainer container) { return toArrayStreamForEach(container.length, container.length); } } 并在declare

中调用您的方法

此处示例:

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;
          }
        });
      });