<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
font-family: "Microsoft YaHei", serif;
}
body, dl, dd, p, h1, h2, h3, h4, h5, h6 {
margin: 0;
}
ol, ul, li {
margin: 0;
padding: 0;
list-style: none;
}
img {
border: none
}
</style>
</head>
<body>
<div id="wrap"></div>
<script>
// 父类
function Person(n,a) {
this.name = n;
this.age = a;
}
Person.prototype.sayInfo = function () {
console.log(`姓名:$(this.name)年龄:$(this.age)`);
};
// 子类
function Teacher(n,a,id) {
// 私有属性的继承
// 子类如果想要用父类的name和age 可以在子类总自执行函数
// 自执行函数this指向window 用call改变this的指向
Person.call(this,n,a);
this.id = id
}
// 原型的继承
// Teacher.prototype = new Person();
// 这里会造成子类的原型中有2个私有属性 undefined
// 所以用这个
function F(){}
F.prototype = Person.prototype;
Teacher.prototype = new F();
// Teacher原型的新增
Teacher.prototype.company = "阿里巴巴";
Teacher.prototype.showID = function () {
console.log(this.id);
};
var knight = new Teacher("knight",18,1508)
</script>
</body>
</html>
Python3Turtle