`
radar
  • 浏览: 27708 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

Re: javascript中基于原型法直接实现继承中的一个陷阱和解决

    博客分类:
  • ajax
阅读更多
<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>
分享到:
评论
1 楼 colinkyo 2008-12-29  
var superClass = arguments[0];  
        var proto = new superClass(Class.isPrototype);  
这是什么意思?

相关推荐

Global site tag (gtag.js) - Google Analytics