【问题标题】:Dojo AMD 1.7 proper inheritance syntax using declareDojo AMD 1.7 使用声明的正确继承语法
【发布时间】:2012-07-23 23:58:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript inheritance dojo js-amd


    【解决方案1】:

    对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”
    《演员》
    “创造”
    “一”
    “乙”

    【讨论】:

    • 成功了,谢谢。比较您的模块定义和我发布的那些,主要区别在于您将模块名称明确设置为“tut.Module1”和“tut.Module2”。这是文档不清楚或我感到困惑的地方。在这个链接中,“dojotoolkit.org/documentation/tutorials/1.7/declare”是一个声明,它说“命名类只有在与 Dojo 解析器一起使用时才应该创建。所有其他类都应该省略 className 参数”。但是在“声明”的文档中,所有示例都使用模块名称。我认为这方面需要澄清。
    • 同意.. 我自己在该领域有过一些挣扎——我相信“继承”函数使用了上述构造函数。_元数据,如果缺少声明的名称,这些数据不会以正确的方式初始化。
    猜你喜欢
    • 2012-02-29
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多