使用混合方式实现javascript中对象的继承(推荐使用)
2013-12-15 14:43
986 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用混合方式实现对象的继承</title>
<script type="text/javascript">
//使用混合方式实现对象的继承(推荐使用)
function Parent(hello) {
this.hello = hello;
}
Parent.prototype.sayHello = function() {
alert(this.hello);
}
function Child(hello,world) {
Parent.call(this, hello);
this.world = world;
}
Child.prototype = new Parent();
Child.prototype.sayWorld = function() {
alert(this.world);
}
var child = new Child("hello", "world");
child.sayHello();
child.sayWorld();
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用混合方式实现对象的继承</title>
<script type="text/javascript">
//使用混合方式实现对象的继承(推荐使用)
function Parent(hello) {
this.hello = hello;
}
Parent.prototype.sayHello = function() {
alert(this.hello);
}
function Child(hello,world) {
Parent.call(this, hello);
this.world = world;
}
Child.prototype = new Parent();
Child.prototype.sayWorld = function() {
alert(this.world);
}
var child = new Child("hello", "world");
child.sayHello();
child.sayWorld();
</script>
</head>
<body>
</body>
</html>
相关文章推荐
- 基于JavaScript实现继承机制之构造函数+原型链混合方式的使用详解
- 基于JavaScript实现继承机制之构造函数+原型链混合方式的使用详解
- javascript使用call方式实现对象继承
- JavaScript使用原型和原型链实现对象继承的方法详解
- 推荐JavaScript实现继承的最佳方式
- JavaScript中实现继承的几种方式的使用和分析
- 使用apply方法实现javascript中的对象继承
- JavaScript实现继承的混合方式
- javascript中实现对象继承的五种方式详解
- JavaScript实现继承机制(4)——构造函数+原型链混合方式
- 使用apply方法实现javascript中的对象继承
- 使用apply方法实现javascript中的对象继承
- JavaScript的弱类对象及继承实现方式
- JavaScript中实现继承的几种方式的使用和分析
- JavaScript里面向对象的继承:不使用构造函数实现"继承"
- 使用apply方法实现javascript中的对象继承
- 详述JavaScript实现继承的几种方式(推荐)
- 基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
- 混合对象 "类" ---js实现继承的两种方式
- 基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解