javascript中的function是万能的,除了用于的函数定义,也可以用于类的定义。javascript的继承,说起来也是有点怪,没有public,private等访问控制修饰,也没有implement或其他特定的符号来说明是实现继承。关于javascript的继承可以参考一下以下这个例子。
<
script type
=
"
text/javascript
"
>
function Person() {
// 属性
this .Gender = " female " ;
this .Age = 18 ;
this .Words = " Silence " ;
// 方法
this .shouting = function () {
alert( " 开心哦!父类的方法 " );
}
}
// 继承
function Programmer() {
this .base = Person;
}
Programmer.prototype = new Person;
// 为子类添加新的方法
Programmer.prototype.typeCode = function () {
alert( " 俺是敲代码的!IT民工,很不开心。子类的方法 " );
}
// 调用示例
function sayHello() {
var a = new Programmer();
alert(a.Gender); // 调用父类的属性
a.shouting(); // 调用父类的方法
a.typeCode(); // 调用子类的方法
}
< / script>
function Person() {
// 属性
this .Gender = " female " ;
this .Age = 18 ;
this .Words = " Silence " ;
// 方法
this .shouting = function () {
alert( " 开心哦!父类的方法 " );
}
}
// 继承
function Programmer() {
this .base = Person;
}
Programmer.prototype = new Person;
// 为子类添加新的方法
Programmer.prototype.typeCode = function () {
alert( " 俺是敲代码的!IT民工,很不开心。子类的方法 " );
}
// 调用示例
function sayHello() {
var a = new Programmer();
alert(a.Gender); // 调用父类的属性
a.shouting(); // 调用父类的方法
a.typeCode(); // 调用子类的方法
}
< / script>