dojo--inherited

java类一样,在dojo里也可以定义constructor 构造函数,在创建一个实例时可以对需要的属性进行初始化。
//定义一个类mqsy_yj

var mqsy_yj = declare(null, {

     // the default username

    username: "yanjun",

     

     // the constructor

     constructor: function(args){

        declare.safemixin(this,args);

    }

});

//实例化
var myins1 = new mqsy_yj();

var myins2 = new mqsy_yj({

    username: "yourname"

});

alert(myins1.username);//yanjun
alert(myins2.username);//yourname
args是一个json结构,可以包含任意多个需要初始化的属性的key:value值对。
this是指当前实例范围,不影响其他实例。
declare.safemixin可能有其他用途,这里使用很简单,不详解。


类似于java的super.method()来调用父类方法,dojo则可以通过this.inherited来实现。

// define class a

var a = declare( null , {

     mymethod: function (){

         console.log( "hello!" );

     }

});

 

// define class b

var b = declare(a, {

     mymethod: function (){

         // call a's mymethod

         this .inherited(arguments); // arguments provided to a's mymethod

         console.log( "world!" );

     }

});

 

// create an instance of b

var myb = new b();

myb.mymethod();

 

 

// would output:

//      hello!

//      world!

this.inherited(arguments);中arguments指的就是在父类中定义的mymethod。

相关文章

我有一个网格,可以根据更大的树结构编辑小块数据.为了更容易...
我即将开始开发一款教育性的视频游戏.我已经决定以一种我可以...
我正在使用带有Grails2.3.9的Dojo1.9.DojoNumberTextBox小部...
1.引言鉴于个人需求的转变,本系列将记录自学arcgisapiforja...
我正在阅读使用dojo’sdeclare进行类创建的语法.描述令人困惑...
我的团队由更多的java人员和JavaScript经验丰富组成.我知道这...