---
title: "Re: javascript中基于原型法直接实现继承中的一个陷阱和解决"
author: "radar"
date: "2007-06-26T11:02:58Z"
tags: []
slug: "re-javascript"
draft: false
lastmod: "2007-06-26T11:02:58Z"
id: 94240
url: "https://radar.iteye.com/blog/94240"
---

```
<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>
```