使用原型
<!DOCTYPE html>
<html>
<head>
<title>JavaScript继承使用原型</title>
</head>
<body>
<script type="text/javascript">
function Item(color,count){
this.color = color;
this.count = count;
if (color == undefined) { this.color = 'black'; }
if (count == undefined) { this.count = 0; }
this.log = function (){
console.log('数量' + this.count + '颜色' + this.color);
}
}
var blueObject = new Item("blue",5);
console.log(blueObject);
function specialItem(name,color,count){
Item.call(this, color,count);
this.name = name;
this.describe = function (){
console.log(this.name + ":color = " + this.color);
}
}
specialItem.prototype = new Item();
var special = new specialItem("Widget","purple",4);
special.log();
special.describe();
console.log(special);
</script>
</body>
</html>