2007-06-26
Re: javascript中基于原型法直接实现继承中的一个陷阱和解决
<SCRIPT LANGUAGE="JavaScript">
<!--
var Class = {
isPrototype: function () {},
create: function() {
return function() {
if (arguments && arguments[0] != Class.isPrototype)
this.initialize.apply(this, arguments);
}
},
extend : function(destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
},
inherit: function () {
var superClass = arguments[0];
var proto = new superClass(Class.isPrototype);
for (var i = 1; i < arguments.length; i++) {
if (typeof arguments[i] == "function") {
var mixin = arguments[i];
arguments[i] = new mixin(OpenLayers.Class.isPrototype);
}
Class.extend(proto, arguments[i]);
if((arguments[i].hasOwnProperty && arguments[i].hasOwnProperty('toString')) ||
(!arguments[i].hasOwnProperty && arguments[i].toString)) {
proto.toString = arguments[i].toString;
}
}
return proto;
}
};
var Base=Class.create();
Base.prototype={
x : 1,
y : 2,
initialize : function(){
this.x=100;
this.y=10;
},
area : function(){
return this.x * this.y
}
}
var Child = Class.create();
Child.prototype=Class.inherit(Base,{
initialize : function(){
Base.prototype.initialize.apply(this);
this.x=10;
},
area : function(){
var old=Base.prototype.area.apply(this);
return old + 100000;
}
});
var base=new Base();
var child=new Child();
alert(child.area())
//-->
</SCRIPT>
发表评论
- 浏览: 11821 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
coder们,陪客户喝酒吗?
flashj 写道05年去一个客户那里搞调研,客户的二号领导中午非要请我们吃饭, ...
-- by yanshiyi -
coder们,陪客户喝酒吗?
05年去一个客户那里搞调研,客户的二号领导中午非要请我们吃饭,到了饭桌,就灌我们 ...
-- by flashj -
coder们,陪客户喝酒吗?
惊鸿逝水 写道酒不敢喝太多,胃不好,俺只能增加个人魅力,牺牲色相了~~ ... ...
-- by lovezqw -
coder们,陪客户喝酒吗?
没陪过
-- by jackyoo8 -
coder们,陪客户喝酒吗?
不喝的飘过
-- by laiseeme






评论排行榜