首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Dojo 中定义跟继承class

2012-10-16 
Dojo 中定义和继承class面向对象,定义Classdojo里如何定义Class:dojo.declare(Customer,null,{construct

Dojo 中定义和继承class
面向对象,定义Class
dojo里如何定义Class:

dojo.declare("Customer",null,{    constructor:function(name){        this.name = name;    },    say:function(){        alert("Hello " + this.name);    },    getDiscount:function(){        alert("Discount is 1.0");    }});var customer1 = new Customer("Mark");customer1.say(); 

如何继承
dojo.declare("VIP",Customer,{    getDiscount:function(){        alert("Discount is 0.8");    }});var vip = new VIP("Mark");vip.say();vip.getDiscount();

使用this.inherited方法调用父类
dojo.declare("VIP",Customer,{    getDiscount:function(){        this.inherited(arguments);        //this.inherited("getDiscount",arguments);    }});

多继承,mixin
  说到继承,多继承的问题又来了。dojo支持多继承,准确地说,是mixin。还记得dojo.declare的第二个参数吗,就是表示父类的那个参数,这个参数可以是一个数组,数组的第一个元素作为声明的类的父类,其他的作为mixin。子类自动获得父类和mixin的所有方法,后面的mixin的同名方法覆盖前面的方法。
dojo.declare("Customer",null,{    say:function(){        alert("Hello Customer");    },    getDiscount:function(){        alert("Discount in Customer");    }});dojo.declare("MixinClass",null,{    say:function(){        alert("Hello mixin");    },    foo:function(){        alert("foo in MixinClass");    }});dojo.declare("VIP",[Customer,MixinClass],{});var vip = new VIP();vip.getDiscount();vip.foo();vip.say();//输出"Hello MixinClass"

其他的比较有用的函数就是dojo.mixin和dojo.extend了,顾名思义,一个是作用于对象实例,一个是用于扩展class
R:http://hi.baidu.com/javajavajava/blog/item/4e8f4bcf8f62cb3bf9dc613a.html

热点排行