欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaScript 的继承

程序员文章站 2022-07-14 17:49:48
...

关于 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>