创建一个新类,使用dom.factory方法,很明显它是一个工厂方法,批量生产各种各式的类。
var MyFirstClass = dom.factory({ message: "hello world", sayHello: function() { alert(this.message); } }); var obj = new MyFirstClass(); obj.sayHello();//hello world
继承:
var Animal = dom.factory({ init: function(name) { this.name = name; }, eat: function() { alert('yummie'); } }); var Human = dom.factory({ inherit: Animal, speak: function() { alert(this.name + ' said bla bla bla'); } }); var peter = new Human('peter'); peter.eat();//yummie peter.speak();//peter said bla bla bla var Pat = dom.factory({ inherit: Human,//注意这里,使用inherit方法 init: function(name,age) { this.age = age } }); var pat = new Pat('pat',18); alert(pat.name)//pat alert(pat.age)//18 pat.speak();//pat said bla bla bla
方法链。就是在当前方法调用其父类的同名方法。
var Girl = dom.factory({ sayHello: function() { return "Hello there"; } }); var FancyGirl = dom.factory( { inherit:Girl, sayHello: function() { return "Well, "+ this.$super() +"!"; } }); var g = new Girl; alert(g.sayHello()); var f = new FancyGirl; alert(f.sayHello());
内部方法:
dom.require("lang"); dom.require("oop") var Person = dom.oop({ init: function(name){ this.name = name; }, secret: function(){ return 'I sometimes like girly drinks'; }.protect(), //定义其为内部方法,只能内部调用 describe: function(){ return "Hi, I'm #{name}. #{secret}. I kid, I kid.".instead({ name: this.name, secret: this.secret() }); } }); var scott = new Person('司徒正美'); // alert(scott.secret());//报错 The method "secret" cannot be called. alert(scott.describe());//Hi, I'm Scott. I sometimes like girly drinks. I kid, I kid.
singleton,标识生产的新类为单例类:
var God = dom.factory({ init:function(name){ this.name = name; this.alertName = function(){ alert(this.name) } }, singleton:true//注意这里,使用singleton属性 }); var god = new God("耶和华"); god.alertName(); //alerts 耶和华 var lucifer = new God("撒旦"); lucifer.alertName(); //alerts 耶和华 alert(god === lucifer )//alerts true
alias,别名机制:
var Array2 = dom.factory({ init:function(){ var args = [].slice.call(arguments); this.setArray(args); }, setArray: function (arr) { //把普通对象变成类数组对象, this.length = 0; //必须要让它成为原型方法才可以使用 [].push.apply(this, arr); return this; }, toString: function () { //返回一个字符串 return [].slice.call(this).toString(); }, valueOf: function () {//获得里面的数组对象 return [].slice.call(this); }, pop:[].pop, push:[].push, shift:[].shift, unshift:[].unshift, reverse:[].reverse, indexOf: function(el,index){ var n = this.length, i = index == null ? 0 : index < 0 ? Math.max(0, n + index) : index; for (; i < n; i++) if (i in this && this[i] === el) return i; return -1; } }); Array2.alias("indexOf","contains") var a = new Array2(1,2,3,4,5); alert(a)//测试toString方法 alert(a.indexOf(3)) alert(a.contains(3))//测试别名机制
include,包含,类似ruby,添加原型成员。
var movable = { run:function(){ alert("能跑") }, fly:function(){ alert("能飞") } } var recognition ={ watch:function(){ alert("看东西") }, smell:function(){ alert("能嗅东西") } } var Robot = dom.factory({ init:function(name,type){ this.name = name; this.type = name; }, include:[movable,recognition] }); var chi = new Robot("小叽","Chobits") ; alert(chi.name); chi.watch(); chi.fly();
extend,扩展,类似ruby,添加类成员。
var MyClass = dom.factory({}); MyClass.extend({ PI:"3.14", alert:function(){ alert("这是静态(类)方法") }, alertPI:function(){ alert(MyClass.PI); } }); MyClass.alert();//这是静态(类)方法 MyClass.alertPI();//3.14 var m = new MYClass m.alert()//报错MYClass is not defined
自扩展与自包含:
var Module = { selfIncluded: function(klass) { klass.prototype.boo = 'boo'; }, selfExtended: function(klass) { klass.BOO = 'BOO'; } }; var MyClass = dom.factory({ include: Module, extend: Module }); alert(MyClass.prototype.boo); // -> 'boo' alert(MyClass.BOO); // -> 'BOO'
dom.geometry = {}; var Point = dom.geometry.Point = dom.factory({ init: function(x, y) { this.x = x*1.0 || 0.0; this.y = y*1.0 || 0.0; }, toString: function() { return this.x + ' ' + this.y; }, clone: function() { return new Point(this.x, this.y); }, eq: function(point) { return this.x == point.x && this.y == point.y; }, //移动点到新的位置 offset: function(x, y) { if (typeof x === 'object') { y = x.y; x = x.x; } this.x += x; this.y += y; return this; }, extend:{ fromString : function(string) { var parts = string.split(/\s+/); return new Point( parts[0], parts[1] ); } } });
添加原型成员。
var movable = { run:function(){ p("能跑") }, fly:function(){ p("能飞") } } var recognition ={ watch:function(){ p("看东西") }, smell:function(){ p("能嗅东西") } } var Robot = oop({ init:function(name,type){ this.name = name; this.type = name; }, include:[movable,recognition] }); var chi = new Robot("小叽","Chobits") ; p(chi.name); chi.watch(); chi.fly();