Dojo AMD 1.7使用declare的正确继承语法

Dojo AMD 1.7 proper inheritance syntax using declare

本文关键字:继承 语法 declare AMD 使用 Dojo      更新时间:2024-01-11

我正在努力解决这个问题:

我在Module1.js:文件中有这个简单的模块

define(['dojo/_base/declare', 'esri/map'], function (declare, map) {
    return declare(null,
        {
            name: "Module 1 name",
            constructor: function() {
                this.name = "module 1";
                //this.age = age;
                //this.currentResidence = currentResidence;
            },

            doSomething: function() {
                alert("calling from Module1");
            }
        });
});

我试图定义一个继承自Module1的Module2,但似乎找不到正确的语法:这就是我目前拥有的:

define(["dojo/_base/declare",
        "esri/map",
        "tut/Module1"],
    function (declare, map, Module1) {
        return declare(null, [Module1],     // or tut/Module1
        {
            name: "Module 2 name",
            constructor: function () {
                this.name = "module 2";
                //this.age = age;                 
            },

            doSomething: function () {
                this.inherited(arguments); // Call superclass method...
                alert("calling from Module 2");
            },
            doSomething1: function () {
                alert("calling do soething 1 from module 2");
            }
        });
});

在我代码的其他地方,我正在这样做:

  require(["tut/Module1", "tut/Module2"], function (Module1, Module2) {        
        var m = new Module1();
        m.doSomething();
        var m2 = new Module2();
        m2.doSomething();    
    }); 

在加载加载dojo的ESRI脚本之前,我定义了以下dojoConfig,如下所示:

     <script type="text/javascript">
   var dojoConfig = {
                async : true,                               
                packages: [{ name: "tut", location: liveString + '/scripts/xxxx/utilities'}]            
            };
 </script>
 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.0compact"></script>

对Module1的调用表明我的包定义是正确的。然而,对Module2的调用抛出一个异常,说明Object(Object Object)没有方法"doSomething"。我已经尝试了许多不同的排列,在手册中来来回回,但找不到正确的语法。现在我只是猜测,所以需要一些帮助。

如何使用Dojo1.7语法从Module1继承?

this.inherited(arguments)的调用将起作用(如果declare不抛出"not a valid mixin",并且两个构造函数方法都不抛出错误)。

尝试

模块1

define(["dojo/_base/declare",
        "esri/map"],
    function (declare, map) {
        return declare("tut.Module1", [], {     // or tut>>.<<Module1
          constructor: function() { this.name='a'; console.log('a ctor'); }
          log: function() { console.log(this.name); }
        });
    }
});

模块2源自模块1

define(["dojo/_base/declare",
        "esri/map",
        "tut/Module1"],
    function (declare, map, Module1) {
        return declare("tut.Module2", [Module1],
          constructor: function() { this.name='B'; console.log('B ctor'); }
          log: function() { this.inherited(arguments); console.log(this.name); }
        });
    }
});

尝试对构造函数进行迭代_模块实例中的meta"bases"("new module().constructor.meta"),这应该会让人了解this.herited是如何工作的

调用

require(["tut/Module2"], function (M2) {
   var m = new M2();
   console.log('created');
   m.log()
});

将输出

"B ctor"
"A ctor"
"已创建"
"A"
"B"