JS 面向对象之继承(原型继承)

JS 面向对象之原型继承

我们知道 Java 是面向对象编程,但其实 JavaScript 也是可以面向对象编程的。

今天介绍一下 JS 基于原型方式的继承。

首先,我们需要定义一个 Student 父类和子类 PrimaryStudent

// 定义Student类
    function Student(props) {
        this.name = props.name || 'Unn amed';
        this.age = props.age || 18;
    }
// 定义Student子类PrimaryStudent
    function PrimaryStudent(props) {
        // 改变Student的this指向(继承Student属性)
        Student.call(this, props);
        this.grade = props.grade || 1;
    }

在子类 PrimaryStudent 中使用了 Student.call(this, props) 来改变父类的 this 指向,这样当我们创建子类对象的时候能拥有父类的属性,对 call 函数不了解的可以参考 MDN文档

接下来我们定义父类和子类的方法。

// 定义Student类的hello方法
    Student.prototype.hello = function () {
        return 'Hello, ' + this.name + '!';
    }
// 定义PrimaryStudent类的方法getGrade:
    PrimaryStudent.prototype.getGrade = function () {
        return this.grade;
    };

将方法绑定在对象原型上可以达到所有对象共用一个方法,从而减小内存的目的。

接下来子类需要继承父类的方法。

	// 原型继承链封装函数
    function inherits(Child, Parent) {
        var F = function () { };
        F.prototype = Parent.prototype;
        Child.prototype = new F();
        Child.prototype.constructor = Child;
    }	
	
    // 原型继承链(继承Student方法):
    inherits(PrimaryStudent, Student);

这里我们使用空函数 F 来连接子类和父类的原型对象。

到此为止,基于原型的继承就做好了,我们来验证一下继承是否成功。

// 父对象
let parent = new Student({ name: '父', age: '28' })
// 子对象
let child = new PrimaryStudent({ grade: 5 })

我们打开浏览器开发者模式(F12)进入 console 控制台输入 child 就能看到 child 对象的全部信息。

在这里插入图片描述

可以看到,子类继承了父类的属性 nameage,由于没有赋值所以取的默认值,同样子类也继承了父类的 hello 方法。

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原生 JavaScript 中,可以通过构造函数和原型链来实现面向对象继承。具体实现方式如下: 1. 构造函数继承 构造函数继承是通过在子类构造函数中调用父类构造函数来实现的。这种方式的缺点是无法继承父类原型上的方法和属性。 ```javascript function Parent(name) { this.name = name; } function Child(name, age) { Parent.call(this, name); this.age = age; } var child = new Child('Tom', 18); console.log(child.name); // Tom console.log(child.age); // 18 ``` 2. 原型继承 原型继承是通过将子类的原型指向父类的实例来实现的。这种方式的缺点是所有子类实例共享父类实例上的属性和方法。 ```javascript function Parent() { this.name = 'parent'; } Parent.prototype.sayHello = function() { console.log('Hello'); }; function Child() {} Child.prototype = new Parent(); var child1 = new Child(); var child2 = new Child(); console.log(child1.name); // parent console.log(child2.name); // parent child1.sayHello(); // Hello child2.sayHello(); // Hello ``` 3. 组合继承 组合继承是将构造函数继承原型继承结合起来使用的一种方式。这种方式既可以继承父类实例上的属性和方法,也可以继承父类原型上的属性和方法。 ```javascript function Parent(name) { this.name = name; } Parent.prototype.sayHello = function() { console.log('Hello'); }; function Child(name, age) { Parent.call(this, name); this.age = age; } Child.prototype = new Parent(); Child.prototype.constructor = Child; var child = new Child('Tom', 18); console.log(child.name); // Tom console.log(child.age); // 18 child.sayHello(); // Hello ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值