继承是一种关系,父类与子类的继承关系,通过原型来实现继承,继承就是为了数据共享.
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function Person(name,money){
this.name=name;
this.money=money;
}
Person.prototype.play=function(){
console.log("会打篮球");
}
var per = new Person("小明","100块");
function Son(score){
this.score=score;
}
Son.prototype=new Person("小小明","100块");
Son.prototype.study=function(){
console.log("踢足球");
}
var son=new Son("考100分");
console.log(son.name,son.money,son.score);
son.study();
son.play();
</script>
</body>
</html>
本文深入探讨了JavaScript中通过原型链实现的继承机制,展示了如何使用构造函数和原型属性创建对象,以及子类如何继承父类的属性和方法。通过具体示例,读者可以了解继承在实际编程中的应用。
334

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



