关于 JavaScript 继承的一个小例子...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo4</title>
</head>
<body>
<script type="text/javascript">
var apple = function(name,color){
this.color = color;
this.name = name;
}
apple.prototype.showColor = function(){
alert("I'm "+this.name+",my color is:"+this.color);
}
var orange = function(name,color){
this.color = color;
this.name = name;;
}
orange.prototype = new apple(this.color,this.name);
var s = new apple("apple","green");
s.showColor();
var b = new orange("orange","orange");
b.showColor();
alert(s.showColor==b.showColor);
</script>
</body>
</html>
本文通过一个简单的JavaScript示例展示了如何实现对象间的继承。该示例创建了两个构造函数apple和orange,并通过原型链的方式让orange继承自apple。示例中包含了显示颜色的方法showColor,用以演示继承的效果。
649

被折叠的 条评论
为什么被折叠?



