JavaScript 继承
- 原型链继承
// An highlighted block
<script>
function Animal(name, sex) {
this.name = name || "动物";
this.sex = sex || "性别";
this.sleep = function () {
return this.name + "睡觉";
}
}
Cat.prototype=new Animal();
Cat.prototype.eat=function (){
return this.name+"吃饭";
}
Cat.prototype.name="小猫";
Cat.prototype.sex="公";
var cat=new Cat();
console.log(cat.name);
Mouse.prototype=new Animal();
Mouse.prototype.name="乔治";
Mouse.prototype.sex="公";
var mouse=new Mouse();
var mouse1=new Mouse();
console.log(mouse.name);
console.log(mouse1.name);
//子类的实例 即是自身 也是父类
console.log(cat instanceof Cat);
console.log(cat instanceof Animal);
console.log(cat);
</script>
注释:prototype 属性共享,给子类原型追加方法或者属性必须在原型继承之后;原型继承不能进行多继承
- 构造继承
// An highlighted block
<script>
function Animal(name, sex) {
this.name = name || "动物";
this.sex = sex || "性别";
this.sleep = function () {
return this.name + "睡觉";
}
}
function AnimalType(type){
this.type=type || "类别";
}
function Cat(name,sex,type){
Animal.call(this,name,sex);
AnimalType.apply(this,[type]);
this.eat=function (){
return this.name+"爱吃老鼠";
}
}
function Mouse(name,sex,type){
Animal.call(this,name,sex);
AnimalType.apply(this,[type]);
this.eat=function (){
return this.name+"爱偷大米";
}
}
var cat=new Cat("小花","公","猫科");
var cat1=new Cat("小黑","母","猫科");
console.log(cat.eat());
console.log(cat1.eat());
console.log(cat instanceof Animal);
console.log(cat);
var mouse=new Mouse("小小","公","鼠科");
console.log(mouse);
</script>
注释:
- 对象里面的this替换 参数的区别 1.替换的对象 2.参数列表 call(this,1,2,3,4,5) apply(this,[1,2,3,4,5,6])
- 在创建子类的实例的时候可以向父类传参,它可以实现多继承,子类的实例只能是自身
- 组合继承
// An highlighted block
<script>
//例一
function Animal(name, sex) {
this.name = name || "动物";
this.sex = sex || "性别";
this.sleep = function () {
return this.name + "睡觉";
}
}
function AnimalType(type) {
this.type = type || "类别";
}
function Cat(type) {
AnimalType.call(this, type);
}
Cat.prototype = new Animal();
Cat.prototype.name = "小花";
function Mouse() {
}
var cat = new Cat("猫科");
console.log(cat);
console.log(cat instanceof Animal);
console.log(cat);
//例二
function Grade(score) {
this.score = score || "0";
}
function Person(name, sex) {
this.name = name || "人";
this.sex = sex || "默认";
}
function Job(job) {
this.job = job || "闲人"
}
function Children(name, sex, job, score) {
Person.apply(this, [name, sex]);
Job.call(this, job);
Grade.call(this, score);
}
var s = new Children("小强", "男", "学生", 485);
var s1 = new Children("小花", "女", "学生", 657);
console.log(s);
console.log(s1);*/
</script>
注释:原型+构造继承可以实现多继承,有数据可以共享;子类的实例即是自身,也是父类
- 实例继承
// An highlighted block
<script>
function Animal(name, sex) {
this.name = name || "动物";
this.sex = sex || "性别";
this.sleep = function () {
return this.name + "睡觉";
}
}
function Cat(name, sex) {
this.eat=function (){
}
var animale = new Animal(name, sex);
return animale;
}
function Mouse() {
}
var cat=new Cat("小猫","公");
console.log(cat);
var cat1=Cat("小花","母");
console.log(cat1);
console.log(cat instanceof Cat);
console.log(cat instanceof Animal);
</script>
注释:不限制调用方式,子类的实例不是本身,而是父类
- 组合寄生继承
// An highlighted block
<script>
function Super(b){
this.b = b;
this.fun = function(){}
}
Super.prototype.c = function(){console.log(1111)}
Super.prototype.name="张三";
function f1(){
}
var f = new f1();
//f.c= Super.prototype.c;
f.prototype= Super.prototype;
//f1.prototype=Super.prototype;
function Foo(a,b){
this.a = a;
Super.call(this,b);
}
Foo.prototype =f;
var foo1 = new Foo(1,2);
console.log(foo1.prototype.c());
</script>
注释:继承时有些模式,拿不到父类的原型方法和属性,考虑寄生模式