1

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

我在文件 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”。我尝试了许多不同的排列,在手册中来回查找,但找不到正确的语法。现在我只是猜测所以需要一些帮助。

如何使用 Dojo 1.7 语法从 Module1 继承?

4

1 回答 1

2

调用this.inherited(arguments)将起作用(如果声明不抛出“不是有效的混合”,并且构造函数方法都不会抛出错误)。

尝试

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

尝试在模块实例('new Module().constructor._meta')中迭代constructor._meta'bases',这应该可以了解this.inherited的工作原理

打电话

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

会输出

“B ctor”
“A ctor”
“创造”
“A”
“B”

于 2012-07-24T10:00:14.470 回答