//定义一个类
function TestClass(name)
{
//定义一个属性,并设置默认值
this.Name = name || "jxh";
//定义一个对象属性
this.internalClass = new InternalClass(this.Name);
//定义一个方法
this.GetFormatName = function()
{
var fname = this.internalClass.FormatName();
return fname;
}
}
//内部类
function InternalClass(name)
{
this.InternalName = name;
}
//继承TestClass的子类
function SubjectClass(name)
{
this.base = TestClass;
this.base(name);
}
继承的时候比较关心的问题就是当父类有引用对象时,父类和子类是不是在操作同一个对象function TestClass(name)
{
//定义一个属性,并设置默认值
this.Name = name || "jxh";
//定义一个对象属性
this.internalClass = new InternalClass(this.Name);
//定义一个方法
this.GetFormatName = function()
{
var fname = this.internalClass.FormatName();
return fname;
}
}
//内部类
function InternalClass(name)
{
this.InternalName = name;
}
//继承TestClass的子类
function SubjectClass(name)
{
this.base = TestClass;
this.base(name);
}
<script language = "javascript">
function Click_Event()
{
var testClass = new TestClass(); //声明父类
var subjectClass = new SubjectClass("jxhwei");//声明子类
var name = "父类:" + testClass.internalClass.InternalName; //调用父类的对象属性
name += "\r\n子类:";
name += subjectClass.internalClass.InternalName;//调用子类的对象属性
alert(name);
}
</script>
运行结果,OK
function Click_Event()
{
var testClass = new TestClass(); //声明父类
var subjectClass = new SubjectClass("jxhwei");//声明子类
var name = "父类:" + testClass.internalClass.InternalName; //调用父类的对象属性
name += "\r\n子类:";
name += subjectClass.internalClass.InternalName;//调用子类的对象属性
alert(name);
}
</script>