JavaScript面向对象继承

发布于 2020-01-09  446 次阅读 本文共1669个字


1、原型继承(是JS中很常用的一种继承方式)
子类children想要继承父类father中的所有的属性和方法(私有+公有),只需要让children.prototype=new father;即可。
特点:它是把父类中私有的+公有的都继承在了子类原型上(子类公有的)
核心:原型继承并不是把父类中的属性和方法克隆一份一模一样的给子类,而是让子类和父类之间增加了原型链的连接,以后子类的实例想要使用父类中的方法,需要一级一级的向上查找来使用

function father() {
  this.x = 100;
};
father.prototype.getX = function() {
  console.log(this.x)
};
function children() {
  this.x = 200;
};
children.prototype = new father;
children.prototype.constructor = children;

2call继承:把父类私有的属性和方法克隆一份一模一样的作为子类私有的属性

function father() {
  this.x = 100;
};
father.prototype.getX = function () {
  console .log(this.x);
};
function children () {
  // this -> fn1
  father.call(this); // father.call(fn1) 把father执行让father中的this变为了fn1
};
var fn1 = new children();
console.log(fn1.x)

3、冒充对象继承:把子类私有的+公有的 克隆一份一模一样的给子类私有的

function father() {
  this.x = 100;
};
father.prototype.getX = function () {
  console.log(this.x)
};
function children() {
  // this -> n
  var temp = new father;
  for (var key in temp) {
    this[key] = temp[key];
  };
  temp = null; 
}
var n = new children;
n.getX();

4、混合模式的继承:原型继承+call继承

function father() {
  this.x = 100;
};
father.prototype.getX = function() {
  console.log(this.x);
};
function children() {
  father.call(this); // n.x = 100
};
children.prototype = new father; // children.prototype: x = 100 getX...
children.prototype.constructor = children;

var n = new children;
n.getX();

5、寄生组合式继承

function father() {
  this.x = 100;
};
father.prototype.getX = function() {
  console.log(this.x);
};
function children() {
  father.call(this);
};
children.prototype = objectCreate(father.prototype);
children.prototype.constructor = children;

var n = new children;
console.dir(n);

6、中间类继承法(不兼容)

function avgFn() {
  arguments.__proto__ = Array.prototype;
  arguments.sort(function(a,b){
    return a-b;
  });
  arguments.pop();
  arguments.shift();
  return eval(arguments.join("+")) / arguments.length;
}
console.log(avgFn(10, 20, 30, 10, 30, 40, 40));


努力,只为遇见更好的自己!