原生js深入理解系列(六)--- JavaScript中的继承超全实现方式

1)对象冒充
对象冒充的实现

红色里面三行代码最关键。相同方法会覆盖
可复制代码,建议各位大大手敲一般会理解更加透彻。

function Parent(username){
	this.username=username;
	this.sayHello=function(){
		console.log(this.username)
	}
}
function Child(username,password){

	this.method=Parent;
	this.method(username)
	delete this.method;
	this.password=password;
	this.sayWorld=function(){
		console.log('password'+this.password)
	}	
}
var parent = new Parent('小明');
var child = new Child('小王','14332432')
parent.sayHello()

child.sayHello()
child.sayWorld()

结果
在这里插入图片描述
2)call方法方式《任何方法都有call()这个方法》

call 方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第二个参数开始,逐一赋给函数中的参数。
在这里插入图片描述
//使用call方式实现对象的继承

function Parent(username){

    this.username=username;

    this.sayHello=function()

    {

        alert(this.username);

    }

}

function Child(username,password){

    Parent.call(this,username);

    this.password=password;

    this.sayWorld=function(){

        alert(this.password);

    }

}

var parent =new Parent("HFJEW");

var child= new Child("lisf","1233");

parent.sayHello();

child.sayHello();

child.sayWorld();

3)applay方法方式!call()方法的作用和 apply() 方法类似,区别就是call()方法接受的是参数列表,而apply()方法接受的是一个参数数组,即第二参数是一个数组

在这里插入图片描述

可复制例子:

function Parent(username) {
    this.username = username;
    this.sayHello = function () {
        console.log(`f-hello-${this.username}`);
        alert(this.username);
    }
}

//挂载在原型链的

//Parent.prototype.getMe = function () {
  //  console.log(`getme-parent-method`)
//}
function Child(username, password) {
    Parent.apply(this, new Array(username))
    this.password = password;
    this.sayWorld = function () {
        console.log(`c-world-${this.password}`)
        alert(this.password);
    }
}

//Child.prototype=new Parent()实现原型链的继承的
var pare = new Parent("ZHASFNSF");
var child = new Child("dfsfd", "3424");
pare.sayHello(); // 这个打印的结果 f-hello-ZHASFNSF
child.sayHello();// 这个打印的结果 f-hello-dfsfd, 这里说明child继承了sayHello函数和username属性,这里的this.username = 'dfsfd'
child.sayWorld(); // 这个打印的结果c-world-3424

如果方法挂载在原型链上的话需要用到原型链的方法继承。即如果给Parent对象增加一个这样的方法:

Parent.prototype.getMe = function () {
    console.log(`getme-parent-method`)
};

到new出来child里调用即:child.getMe()会报错

如果想解决这个报错就得使用下面方法5.混合方式了

4)通过原型链的方式实现对象继承,其中最关键一句就是child1.prototype = new Parent1(),这一句就是实现了继承的功能
在这里插入图片描述

5)混合方式《推荐》,这里是原型链方式和call()方法一起实现继承,即属性就使用apply()继承,方法就使用原型链继承。
在这里插入图片描述

创建对象,用原型扩展方法,继承等的练习。实现测三角形面积和矩形面积和边为多少。下面是实例:

<title></title>

其他的方法

在这里插入图片描述

7)es6方法实现

class Human{
  // 构造函数
  constructor(props) {
     this.name = props.name || '王二小'  
  }
  eat () {
    alert(this.name+'吃饭。。。')
  }
}

class Man extends Human {
     //构造函数
    constructor(props) {
        //调用实现父类的构造函数
        super(props);
        this.type = props.type || '工程师'
    }
    work () {
        alert(this.name+'在工作') 
    }
}

var newMan = new Man({
    name: '刘大山'
})
newMan.eat()
newMan.work()

欢迎大家指点一起交流!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值