论坛首页 Web前端技术论坛

写了一个JS的extend实现,可能还有bug……抛砖而已

浏览 7713 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-08-21  
var VM = {}
VM._$counter = 1;
VM.getUID = function(); {return "_Ox" + this._$counter++;}

Function.prototype.inherits = function(superclass); {
	//_debugger.output(this._$hashcode, "<BR>");;
	if (this._$superclass);
		throw new Error("Syntax error on extend, multiple inheritence is not allowed");;

	if (!isClass(superclass););
		throw new Error("Syntax error on extend, only class could be extended");;
	if (!superclass._$hashcode);
		superclass._$hashcode = VM.getUID(superclass);;
	if (!this._$hashcode);
		this._$hashcode = VM.getUID(this);;

	if (this == superclass || this._$hashcode == superclass._$hashcode);
		throw new Error("Syntax error on extend, inheritence based on the class self is not allowed");;	
	if (this._$derived && this._$derived[superclass._$hashcode]);
		throw new Error("Syntax error on extend, recursive inheritence is not allowed");;
	if (superclass._$derived && superclass._$derived[this._$hashcode]);
		throw new Error("Syntax error on extend, duplicated inheritence occured");;

	for (var key in superclass); {
		if (!this[key] && !(key.indexOf("_$"); == 0););
			this[key] = superclass[key];
	}
	
	this._$superclass = superclass;
	
	var ite = superclass;
	while (ite); {
		if (!ite._$derived);
			ite._$derived = [];

		if (ite._$derived[this._$hashcode]); {
			//_debugger.output(this._$hashcode + " : " + ite._$hashcode + " : " + ite._$derived[this._$hashcode], "popup");;
			//_debugger.output(ite._$derived[this._$hashcode], "popup");;
			throw new Error("Syntax error on extend, duplicated inheritence occured");;
		}
		else {
			ite._$derived[this._$hashcode] = this;
		}
		ite = ite._$superclass;
	}

	if (!this.prototype._$class);
		this.prototype._$class = this;
	this.prototype._$superclass = superclass;
	this.prototype.$super = function(); {
		var arg = arguments;
		if (!this._$superclass);
			return null;
		if (!this._$super); {
			var SuperConstructor = this._$superclass.prototype.constructor;
			this._$super = new SuperConstructor(arg[0], arg[1], arg[2], arg[3], arg[4]);;

			for (var key in this._$super); {
				if (!this[key] && !(key.indexOf("_$"); == 0););
					this[key] = this._$super[key];
			}
		}

		return this._$super;
	}
	return this;
}


Example:
var ParentClass = function(name, title); {
  this.name = name;
  this.title = title;
  this.getFullName = function(); {return this.title + " " + this.name;}
}
ParentClass.demoClassMethod = function(); {
  window.alert("I am a class method");;
}

var ChildClass = function(name, title, age); {
  this.$super(name, title);;
  this.age = age;
  this.getFullName = function(); {return this.$super();.getFullName(); + "[Age:" + this.age + "]";}
}
//essence here :);
ChildClass.inherits(ParentClass);;


var parentObj = new ParentClass("Jack", "Mr.");;
var childObj = new ChildClass("Alice", "Mrs.", 30);;

assert(childObj.getFullName(); == "Mrs. Alice[Age:30]");;
assert(ChildClass.demoClassMethod && ChildClass.demoClassMethod(); == "I am a class method");;

inherits方法作的事情:
1、将父类的类成员属性/方法,copy至子类中。(literal properties是值copy,成员对象及方法为引用copy)
2、在子类的prototype中添加一个$super方法,该方法第一次调用为创建一个父类的实例对象,之后调用将返回该实例对象。
3、所有以"_$"开头的成员属性/方法将被认为是内部成员,不做处理

使用:
在子类的constructor方法中,确保第一句为this.$super(arg1,arg2,arg3...)。否则将无法实现实例对象属性/方法的copy。

基本上这个$super实现的东西类似于java中的super,只是我现在找不到好的办法限制它的scope,以至于在Class方法体外部也可以访问到。

考虑还不是很充分,所以肯定是有些问题的……只是希望能为研究JS enhancement的朋友提供一些思路
   发表时间:2005-08-22  
看得出花了不少心血。

不过javascript 就是 javascript,我觉得不是很有必要套用java的编码模式。毕竟它是动态脚本语言,用java的思维写javascript常常会限制javascript的创意。

以前sourceforge上有一个叫ie7的项目,设计好象类似,现在没什么动静了,而且真正的ie7也快要出现了。
0 请登录后投票
   发表时间:2005-08-22  
醒来 写道
看得出花了不少心血。

不过javascript 就是 javascript,我觉得不是很有必要套用java的编码模式。毕竟它是动态脚本语言,用java的思维写javascript常常会限制javascript的创意。

以前sourceforge上有一个叫ie7的项目,设计好象类似,现在没什么动静了,而且真正的ie7也快要出现了。


同意,所以我只是写了个简单的东西贴上来,或许能给正在钻研JS的朋友一点启发而已:)

真正更多的OO支持,还是要看JS2.0阿,强烈关注FF更新版本中
0 请登录后投票
   发表时间:2005-08-23  
"真正更多的OO支持,还是要看JS2.0阿,强烈关注FF更新版本中"

为什么要等到js2.0 ? 不是可以直接在js中调用类 和接口吗 ?
0 请登录后投票
   发表时间:2005-08-23  
51js 上面关于 jsvm 的讨论:
http://www.51js.com/viewthread.php?fpage=1&tid=31592
论坛上以前关于 jsvm 的讨论:
http://forum.iteye.com/viewtopic.php?t=7252
其实这些事情以前已经有人做过了。不过我也是在等 JS2.0 中 class 的引入。
winterwolf 写道
为什么要等到js2.0 ? 不是可以直接在js中调用类 和接口吗 ?

你说的是浏览器端 JS 的还是服务器端的 JS?如果是浏览器端的,目前还没有这些东西。
0 请登录后投票
   发表时间:2005-08-23  
dlee 写道
51js 上面关于 jsvm 的讨论:
http://www.51js.com/viewthread.php?fpage=1&tid=31592
论坛上以前关于 jsvm 的讨论:
http://forum.iteye.com/viewtopic.php?t=7252
其实这些事情以前已经有人做过了。不过我也是在等 JS2.0 中 class 的引入。
winterwolf 写道
为什么要等到js2.0 ? 不是可以直接在js中调用类 和接口吗 ?

你说的是浏览器端 JS 的还是服务器端的 JS?如果是浏览器端的,目前还没有这些东西。


jsvm是个学习的好材料:)
可惜,51js鱼龙混杂,这个项目当初也没有宣传出去……
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics