// 提前写好的JavaScript Class定义和继承// 当然,这种代码很丑陋,散发着代码的坏味道。function BaseClass() { //BaseClass constructor code goes here }BaseClass.prototype.getName = function() { return "BaseClass";}function SubClass() { //SubClass constructor code goes here }//Inherit the methods of BaseClassSubClass.prototype = new BaseClass();//Override the parent's getName methodSubClass.prototype.getName = function() { return "SubClass";}//Alerts "SubClass"alert(new SubClass().getName());
// 运行时的JavaScript Class 定义和继承// 看上去很传统,但这些脚本会导致在Internet Explorer中的内存泄漏.function BaseClass() { this.getName = function() { return "BaseClass"; }; //BaseClass constructor code goes here }function SubClass() { //在对象实例建立时重载父类的getName方法 this.getName = function() { return "SubClass"; } //SubClass constructor code goes here }//Inherit the methods of BaseClassSubClass.prototype = new BaseClass();//Alerts "SubClass"alert(new SubClass().getName());