|
@@ -33,17 +33,32 @@
|
|
// console.log(s1.userName);
|
|
// console.log(s1.userName);
|
|
// console.log(s1.loveCoding);
|
|
// console.log(s1.loveCoding);
|
|
|
|
|
|
- // 原型继承
|
|
|
|
|
|
+ // 原型继承 无法继承构造函数中的属性和方法
|
|
|
|
+ // function Student(userName,userAge,school) {
|
|
|
|
+ // // this.userName = userName;
|
|
|
|
+ // // this.userAge = userAge;
|
|
|
|
+ // this.school = school;
|
|
|
|
+ // }
|
|
|
|
+ // Student.prototype = new Person();
|
|
|
|
+ // let s1 = new Student('张三',18,'清华大学');
|
|
|
|
+ // console.log(s1.loveCoding);
|
|
|
|
+ // console.log(s1.userName);
|
|
|
|
+ // s1.talk();
|
|
|
|
+
|
|
|
|
+ // 组合继承
|
|
function Student(userName,userAge,school) {
|
|
function Student(userName,userAge,school) {
|
|
- this.userName = userName;
|
|
|
|
- this.userAge = userAge;
|
|
|
|
|
|
+ // 构造函数继承 可以继承到构造函数中的属性和方法
|
|
|
|
+ Person.call(this,userName,userAge);
|
|
this.school = school;
|
|
this.school = school;
|
|
}
|
|
}
|
|
|
|
+ // 原型继承 可以继承到原型上的属性和方法
|
|
Student.prototype = new Person();
|
|
Student.prototype = new Person();
|
|
|
|
+ Student.prototype.constructor = Student;
|
|
let s1 = new Student('张三',18,'清华大学');
|
|
let s1 = new Student('张三',18,'清华大学');
|
|
console.log(s1.loveCoding);
|
|
console.log(s1.loveCoding);
|
|
console.log(s1.userName);
|
|
console.log(s1.userName);
|
|
- s1.talk();
|
|
|
|
|
|
+ console.log(s1.constructor);
|
|
|
|
+
|
|
</script>
|
|
</script>
|
|
</body>
|
|
</body>
|
|
|
|
|