当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript面向对象继承原理与实现方法分析

JavaScript面向对象继承原理与实现方法分析

2018年08月19日  | 移动技术网IT编程  | 我要评论

本文实例讲述了javascript面向对象继承原理与实现方法。分享给大家供大家参考,具体如下:

1、构造函数、原型和实例的关系

构造函数有一个原型属性prototype指向一个原型对象。

原型对象包含一个指向构造函数的指针constructor

实例包含一个指向原型对象的内部指针[[prototype]]

2、通过原型链实现继承

基本思想:利用原型让一个引用类型继承另一个引用类型的属性和方法,子类型可以访问超类型的所有属性和方法。原型链的构建是将一个类型的实例赋值给另一个构造函数的原型实现的。实现的本质是重写原型对象,代之以一个新类型的实例。

function person(name) {
  this.name = name;
}
person.prototype.sayhello = function() {
  alert("hello, " + this.name);
}
var person = new person("alice");
person.sayhello(); // hello, alice
function student() {
}
student.prototype = new person("bruce");
student.prototype.id = 16;
student.prototype.showid = function() {
  alert(this.id);
}
var student = new student();
student.sayhello(); // hello, bruce
student.showid(); // 16

注意:不能用对象字面量创建原型方法,这样会重写原型链,导致继承无效。

function person(name) {
  this.name = name;
}
person.prototype.sayhello = function() {
  alert("hello, " + this.name);
}
var person = new person("alice");
person.sayhello(); // hello, alice
function student() {
}
student.prototype = new person("bruce");
student.prototype.id = 16;
student.prototype = {
  showid: function() {
    alert(this.id);
  }
};
var student = new student();
student.sayhello(); // 报错:student.sayhello is not a function
student.showid(); // 16

student指向student的原型,student的原型又指向person的原型。

student.sayhello()原型链搜索机制:

1)搜索student实例中是否有sayhello()

2)搜索student.prototype是否有sayhello()

3)搜索person.prototype是否有sayhello()

子类型有时候需要覆盖超类型的某个方法,或者需要添加超类型中不存在的某个方法。

function person(name) {
  this.name = name;
}
person.prototype.sayhello = function() {
  alert("hello, " + this.name);
}
var person = new person("alice");
person.sayhello(); // hello, alice
function student() {
}
student.prototype = new person("bruce");
student.prototype.id = 16;
student.prototype.showid = function() {
alert(this.id);
}
student.prototype.sayhello = function() {
  alert("hi, " + this.name);
}
var student = new student();
student.sayhello(); //hi, bruce
student.showid(); // 16

注意:给原型覆盖或添加方法的代码一定要放在替换原型的语句之后。

function person(name) {
  this.name = name;
}
person.prototype.sayhello = function() {
  alert("hello, " + this.name);
}
var person = new person("alice");
person.sayhello(); // hello, alice
function student() {
}
student.prototype.sayhello = function() {
  alert("hi, " + this.name);
}
student.prototype = new person("bruce");
student.prototype.id = 16;
student.prototype.showid = function() {
alert(this.id);
}
var student = new student();
student.sayhello(); // hello, bruce
student.showid(); // 16

确定实例和原型的关系:

(1)instanceof

alert(student instanceof object); // true
alert(student instanceof student); // true
alert(student instanceof person); // true

(2)isprotptypeof

alert(object.prototype.isprototypeof(student)); // true
alert(student.prototype.isprototypeof(student)); // true
alert(person.prototype.isprototypeof(student)); // true

(3)getprototypeof

object.getprototypeof(student1) == student.prototype

使用原型链实现继承的问题:

(1)引用类型的属性会被实例共享,原型实现继承时,原型会变成另外一个类型的实例,实例的属性则变成了现在的原型属性,从而被共享。

function person(name, age) {
  this.friends = ["cindy","david"];
}
function student() {
}
student.prototype = new person();
var student1 = new student();
student1.friends.push("bruce");
alert(student1.friends); // "cindy","david","bruce"
var student2 = new student();
alert(student1.friends); // "cindy","david","bruce"

(2)在创建子类型的实例时,不能向超类型的构造函数中传递参数,实际上,应该是没有办法在不影响所有对象实例的情况下,给超类型的构造函数传递参数。

实际中很少单独使用原型链实现继承。

3、通过构造函数实现继承

基本思想:在子类型构造函数的内部调用超类型构造函数。通过使用apply()call()方法也可以在新创建的对象上执行构造函数。

function person(name, age) {
  this.name = name;
  this.age = age;
}
function student() {
  person.call(this,"alice",22); // 继承了构造函数person,同时还传递了参数
  this.id = 16; // 实例属性
}
var student = new student();
alert(student.name); // "alice"
alert(student.age); // 22
alert(student.id); // 16

使用构造函数实现继承的问题:

(1)在超类型的原型中定义的方法,对子类型而言是不可见的,结果所有类型都只能使用构造函数模式。

(2)要想子类能够访问超类定义的方法,方法只能在构造函数中定义,但方法在构造函数中定义时,函数复用无从谈起。

function person(name, age) {
  this.name = name;
  this.age = age;
}
person.prototype.showname = function() {
  alert(this.name);
};
function student() {
  person.call(this,"alice",22);
  this.id = 16;
}
var student = new student();
alert(student.showname()); // 报错:student.showname is not a function

实际中很少单独使用使用构造函数实现继承。

4、组合使用原型链和构造函数实现继承

思路:使用原型链继承共享的属性和方法,使用构造函数继承实例属性。

效果:既通过在原型上定义方法实现了函数复用,又能够保证每个实例都有自己的属性。

function person(name, age) {
  this.name = name;
  this.age = age;
  this.friends = ["cindy","david"];
}
person.prototype.sayhello = function() {
  alert("hello, " + this.name);
}
function student(name, age, id) {
  person.call(this, name, age);
  this.id = id;
}
student.prototype = new person();
student.prototype.showid = function() {
  alert(this.id);
}
var student1 = new student("alice", 22, 16);
student1.friends.push("emy");
alert(student1.friends); // "cindy","david","emy"
student1.sayhello(); // hello, alice
student1.showid(); // 16
var student2 = new student("bruce", 23, 17);
alert(student2.friends); // "cindy","david"
student2.sayhello(); // hello, bruce
student2.showid(); // 17

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网