web前端全系列 教程
1839个小节阅读:2243.5k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
面向对象编程很重要的一个方面,就是对象的继承。A 对象通过继承 B 对象,就能直接拥有 B 对象的所有属性和方法。这对于代码的复用是非常有用的。
大部分面向对象的编程语言,都是通过“类”(class)实现对象的继承。传统上,JavaScript 语言的继承不通过 class,而是通过“原型对象”(prototype)实现
温馨提示
ES6版本已经提供了 class 语法,我们会在讲解ES6的时候单独讲解使用方式
xxxxxxxxxx
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getName = function () {
console.log(this.name);
}
Person.prototype.getAge = function(){
console.log(this.age);
}
function Student(name, age, major) {
//调用父类的构造函数
Person.call(this, name, age);
this.major = major;
}
// 继承父类原型中的方法
for (var p in Person.prototype) {
Student.prototype[p] = Person.prototype[p];
}
Student.prototype.showMajor = function () {
console.log(this.major);
}
Student.prototype.getName = function(){
console.log("Student:" + this.name);
}
var student = new Student("itbaizhan", "20", "it");
student.showMajor();
student.getName();
student.getAge();
实时效果反馈
1. 下列是子类继承父类,横线处应该填写代码是:
xxxxxxxxxx
function Student(name, age, major) {
//调用父类的构造函数
Person.___(this, name, age);
this.major = major;
}
A apply
B call
C bind
D this
答案
1=>B