|
@@ -0,0 +1,45 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <script>
|
|
|
+ // 构造函数
|
|
|
+ function Person(name,age){
|
|
|
+ this.username = name;
|
|
|
+ this.age = age;
|
|
|
+ }
|
|
|
+ // 原型上写方法
|
|
|
+ Person.prototype.talk = function(){
|
|
|
+ console.log(`我叫${this.username},我今年${this.age}岁`)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 继承
|
|
|
+ function Teacher(name,age,school ){
|
|
|
+ // 调用父类的构造函数
|
|
|
+ Person.call(this,name,age);
|
|
|
+ this.school = school;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 继承父类的方法
|
|
|
+ Teacher.prototype = Person.prototype;
|
|
|
+ // 修复构造函数指向
|
|
|
+ Teacher.prototype.constructor = Teacher;
|
|
|
+ // 自己独有的方法
|
|
|
+ Teacher.prototype.showSchool = function(){
|
|
|
+ console.log(`我是${this.username},我来自${this.school}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 实例化对象
|
|
|
+ let t1 = new Teacher("王五",30,"清华大学");
|
|
|
+ console.log(t1.username)
|
|
|
+ t1.talk();
|
|
|
+ t1.showSchool();
|
|
|
+ console.log(t1.constructor);
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|