原型链继承
特点:
1.js继承是把父类的原型放到子类的原型链上,实例想要调用这些方法,其实是基于__proto__原型链的机制查找完成的
2.子类可以重写父类上的属性和方法
3.父类中私有的或者公有的属性和方法, 最后都会变成子类公有的属性和方法
//父类
function A(x){
this.x=x
}
A.prototype.getX=function(){
console.log(this.x)
}
//子类
function B(y){
this.y=y
}
B.prototype.new A(100)
B.prototype.constructor=B
B.prototype.getY=function(){
console.log(this.y)
}
let b =new B(200)
b.x
b.getX()
console.log(b)
寄生组合继承
结合原型链继承和call继承的方法,同时自己创建一个对象,并且让这个对象的原型指向父类构造函数的prototype.实现寄生组合继承
特点:
1.最完美的JS继承解决方案
2.父类私有的属性和方法,子类实例私有的属性和方法
3.父类公有的属性和方法,子类实例公有的属性和方法
4 .子类实例修改公有的属性和方法不会影响父类的实例
//父类
function A(x) {
this.x = x;
this.seyHello = function () {
console.log("hello world");
}
}
A.prototype.getX = function () {
console.log(this.x);
}
//子类
function B(y, x) {
this.y = y;
A.call(this, x);
}
/* let obj = {};
obj.__proto__ = A.prototype
//因为IE浏览器不支持用户在js中直接使用或者修改__proto__
B.prototype = obj*/
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
B.prototype.getY = function () {
console.log(this.y);
}
let b = new B(200, 100);
console.log(b);
// Object.create(arg) 创建空的对象,并且让这个空对象的__proto__指向传递的第一个参数
Object.create = function (arg){
//因为IE浏览器不支持用户在js中直接使用或者修改__proto__
/*let obj = {};
obj.__proto__ = arg;
return obj*/
function NewObj(){}
NewObj.prototype = arg;
return new NewObj()
}
组合继承
结合原型链继承和借用构造函数继承组合起来实现的继承
特点:
1.子类实例可以使用父类的私有属性和方法
2.父类私有的属性和方法也会变成子类实例私有的属性和方法
3.子类实例父类公有的属性和方法
4.子类的原型链上会存在一份多余的父类私有属性
call继承
在子类构造函数中把父类构造函数当作普通的函数执行, 并且通过call方法把父类构造函数中的this替换成子类的实例(this), 这样相当于给子类实例设置了私有的属性和方法
特点:
1.只能继承父类的所有的私有属性和方法(因为只是把Parent当作普通函数执行了一次,跟Parent 的原型上的方法和属性没有任何关系)
2.父类私有的属性和方法都会变成子类私有的属性和方法
ES6中的class继承
// ES5中的类(class) => 构造函数(constructor)
// function Parent(x){
// this.x = x;
// this.sayHello = function (){
// console.log("sayHello")
// }
// }
// Parent.prototype.sx= "属性";
// Parent.prototype.getX= function (){
// console.log("getX==>",this.x)
// }
// let p1 = new Parent(100);
/*
{//ES6中的类
class Parent {
constructor(x) {
this.x = x;
this.sayHello = function () {
console.log("sayHello")
}
}
// sx: "属性",
getX() {
console.log("getX==>", this.x)
}
}
//公有属性
Parent.prototype.name = "Parent";
//公有方法
// Parent.prototype.getX= function (){
// console.log("getX==>",this.x)
// }
let p1 = new Parent(100)
}
*/
//ES6中的继承
/* class Parent {
constructor(x) {
this.x = x;
this.sayHello = function () {
console.log("sayHello")
}
}
getX() {
console.log("getX==>", this.x)
}
}
class Child {
constructor(y) {
//在ES6的class没有办法直接当作一个普通函数执行
// Child(123) //Uncaught TypeError: Class constructor Child cannot be invoked without 'new' //必须使用new关键字来创建实例,不能当作一个普通函数执行
// Parent.call(this,100)
this.y = y;
}
getY(){
console.log("getY==>", this.y)
}
}
//在使用ES6的继承的时候,没有办法直接重定向子类的原型
// Child.prototype = Object.create(Parent.prototype);
let c1 = new Child(200);
*/
//ES6中的继承
//通过extends来实现的继承
//ES6继承 ==>class 子类构造函数 extends 父类构造函数{}
//Child.prototype.__proto__ = Parent.prototype;
/*
* 1. 父类的私有属性和方法,会成为子类的私有属性和方法
* 2.
* */
class Parent {
constructor(x) {
this.x = x;
this.sayHello = function () {
console.log("sayHello")
}
}
getX() {
console.log("getX==>", this.x)
}
}
class Child extends Parent {
//如果不写constructor,不会报错,继承会正常继承
//如果不写constructor浏览器会自动的帮我们去创建以下代码
// constructor(...args){
// super(...args)
//