<script> | |
function Student(name,age,sex){ | |
this.name = name; | |
this.age = age; | |
this.sex = sex; | |
this.arr = [1,2,3]; | |
this.a = 10; | |
} | |
Student.prototype.study = function(){console.log("沉迷学习")}; | |
function Primary(){ | |
this.play = function(){console.log("打游戏")}; | |
} | |
/* 原型链继承:让子类构造函数的原型指向父类的实例对象 | |
缺点:无法传参,创建的对象单一,如果父类中有引用类型(复杂类型)的数据,就会一改全改 | |
*/ | |
Primary.prototype = new Student("小明",8,"男"); | |
var p1 = new Primary(); | |
console.log(p1); | |
console.log(p1.name); | |
p1.play(); | |
p1.study(); | |
var p2 = new Primary(); | |
console.log(p2); | |
console.log(p2.name); | |
p1.arr.push(4); | |
console.log(p1.arr); | |
console.log(p2.arr); | |
p1.a = 30; | |
console.log(p1.a);//30 | |
console.log(p2.a);//10 | |
</script> |
原型链继承
最新推荐文章于 2024-07-15 22:21:55 发布