类的创建与继承

在面向对象(oo)编程中,类(class)是对象(object)的模板,定义了同一组对象(实例)共有的属性和方法。JavaScript语言里没有类的概念,但是我们通过以下方法也可以模拟出类。

一.创建类:
(1)构造函数

function Person(name,age){
    this.name = name;
    this.age = age;
    this.sayName = function () {
        alert(this.name);
    }
}
var person = new Person('nike',23);
console.log(person.name,person.age);
person.sayName();
//nike 23

(2)利用原型

function Person(name,age){
    this.name = name;
    this.age = age;
}
Person.prototype = {
    constructor: Person,
    sayName: function () {
        alert(this.name)
    }
};
var person = new Person('nike',23);
console.log(person.name,person.age);
person.sayName();
//nike 23

(3)利用原型式继承,Object.create()方法

var person = {
    name: 'nike',
    friends: ['a','b']
};
var person1 = Object.create(person);
person1.name = 'amy';
person1.friends.push('d');
console.log(person1.name,person1.friends);
//amy 
// ["a", "b", "d"]

二.类的继承
(1)原型链继承

function SuperType () {
    this.property = true;
}
SuperType.prototype.getSuperValue = function(){
    return this.property;
};



function SubType(){
    this.subproperty = false;
}
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function(){
    return this.subproperty;
};

var instance = new SubType();
console.log(instance.getSuperValue());  //true
console.log(instance.getSubValue());  //false

(2)借用构造函数继承
用原型链继承有一个问题:如果原型属性值是引用类型值的话,包含引用类型值的原型属性会被所有实例共享,为解决这一问题,在子类型构造函数的内部调用超类型构造函数。这样,原型的每个实例都会具有自己的属性副本了。

function SuperType () {
    this.color = ['a','b','c']
}

function SubType(){
    SuperType.call(this);
}
SubType.prototype = new SuperType();

var instance1 = new SubType();
var instance2 = new SubType();

instance1.color.push('d');
console.log(instance1.color);// ["a", "b", "c", "d"]
instance2.color.push('e');
console.log(instance2.color);//["a", "b", "c", "e"]

但是采用构造函数模式的话,方法都在构造函数中定义,就无法复用了。
(3)组合继承
原型链继承和构造函数继承的组合体。
通过调用父类构造,继承父类的属性并保留传参的优点
然后通过将父类实例作为子类原型,实现函数复用。

function SuperType (name) {
    this.name = name;
    this.color = ['a','b','c']
}
SuperType.prototype.sayName = function(){
    alert(this.name);
};

function SubType(name,age){
    SuperType.call(this,name);
    this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function(){
    alert(this.age);
};

var instance1 = new SubType('nike',23);
instance1.color.push('d');
console.log(instance1.color);
instance1.sayName();

var instance2 = new SubType('amy',22);
instance2.color.push('e');
console.log(instance2.color);
instance2.sayName();

但是这种方法调用了两次父类构造函数,生成了两份实例。
(4)寄生组合继承
通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。
即: 不必为了指定子类型的原型去调用超类型的构造函数,我们只需要超类型的一个副本而已。

function inheritProtoType(SubType,SuperType){
    var prototype = Object.create(SuperType.prototype);
    prototype.constructor = SubType;
    SubType.prototype = prototype;
}

function SuperType (name) {
    this.name = name;
    this.color = ['a','b','c']
}
SuperType.prototype.sayName = function(){
    alert(this.name);
};

function SubType(name,age){
    SuperType.call(this,name);
    this.age = age;
}
//省去了第一次调用。
inheritProtoType(SubType,SuperType);
SubType.prototype.sayAge = function(){
    alert(this.age);
};

var instance1 = new SubType('nike',23);
instance1.color.push('d');
console.log(instance1.color);
instance1.sayName();

var instance2 = new SubType('amy',22);
instance2.color.push('e');
console.log(instance2.color);
instance2.sayName();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值