日期:2014-05-16  浏览次数:20349 次

JavaScript 学习笔记十 练习任务系统
//开发一个任务系统,我们需要抽象出一个类来表示任务,对应的,
//每个任务都可能会有一个监听器,当任务执行之后,需要通知监听器。
//监听器的类
var EventListener = Base.extend({
    constructor : function(sense){
        this.sense =sense;
    },
    sense : null,
    handle : function(){
        logs(this.sense+"  occured");
    }
});
//任务类
var Task = Base.extend({
    constructor : function(name){
        this.name = name;
    },
    name : null,
    listener : null,
    execute : function(){
        logs(this.name);
        this.listener.handle();
    },
    setListener : function(listener){
        this.listener = listener;
    }
});
//创建类的方式很简单,需要给 Base.extend 方法传入一个 JSON 对象
//方法访问自身的成员时需要加 this 关键字
var pri = new Task("定时任务1");
var priListen = new EventListener("监听器1");
pri.setListener(priListen);
pri.execute();
//打印如下:
  //定时任务1
  //监听器1 occured

?

1 楼 sblig 2012-05-22  
//Base.js
var Base = function() {};
Base.prototype = {
	extend : function(source) {
		if (arguments.length > 1) { 
			var ancestor = this[source];
			var value = arguments[1];
			if (typeof value == "function" && ancestor
					&& /\bbase\b/.test(value)) {
				var method = value;
				value = function() {
					var previous = this.base;
					this.base = ancestor;
					var returnValue = method.apply(this, arguments);
					this.base = previous;
					return returnValue;
				};
				value.method = method;
				value.ancestor = ancestor;
			}
			this[source] = value;
		} else if (source) { 
			var extend = Base.prototype.extend;
			if (Base._prototyping) {
				var key, i = 0, members = ["constructor", "toString", "valueOf"];
				while (key = members[i++]) {
					
					if (source[key] != Object.prototype[key]) {
						extend.call(this, key, source[key]);
					}
				}
			} else if (typeof this != "function") {
				extend = this.extend || extend;
			}
			for (key in source)
				if (!Object.prototype[key]) {
					extend.call(this, key, source[key]);
				}
		}
		return this;
	},
	base : Base
};

Base.extend = function(_instance, _static) { 
	var extend = Base.prototype.extend;
	Base._prototyping = true;
	var proto = new this;
	extend.call(proto, _instance);
	delete Base._prototyping;
	var constructor = proto.constructor;
	var klass = proto.constructor = function() {
		if (!Base._prototyping) {
			if (this._constructing || this.constructor == klass) { 
				this._constructing = true;
				constructor.apply(this, arguments);
				delete this._constructing;
			}
			else { 
				var object = arguments[0];
				if (object != null) {
					(object.extend || extend).call(object, proto);
				}
				return object;
			}
		}
	};
	for (var i in Base) {
		klass[i] = this[i];
	}
	klass.ancestor = this;
	klass.base = Base.base;
	klass.prototype = proto;
	klass.toString = this.toString;
	extend.call(klass, _static);
	if (typeof klass.init == "function")
		klass.init();
	return klass;
};

Base = Base.extend({
	constructor : function() {
		this.extend(arguments[0]);
	}
   }, 
   {
	ancestor : Object,
	base : Base,
	implement : function(_interface) {
		if (typeof _interface == "function") {
			_interface(this.prototype);
		} else {
			this.prototype.extend(_interface);
		}
		return this;
	}
  });