与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。
原文地址:https://blog.csdn.net/qq_29009901/article/details/88310717
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。