JS简单继承

继承:子类去继承父类的公有属性和私有属性
1、原型继承
特点:  可以继承父类的公有属性和私有属性
Child.prototype = new Parent;
<script>
	 function Parent() {  // 父类
        this.x = 100;
    }
    Parent.prototype.getX = function () {
        return this.x;
    }
    function Child() {  // 子类
        this.y = 200;
    }
    Child.prototype = new Parent; // 让子类的原型指向父类对象
    Child.prototype.getY = function () {
        return this.y;
    }
    // 对于Child来说。它继承了
    var c1 = new Child();
    console.log(c1.x); 	//100 x是继承父的x
    console.log(c1.getX()); 	//100 getX是继承父的x
    console.log(c1.y); 	//200
    console.log(c1.getY()); 	//200
</script>

2、call继承
特点:只能继承父的私有属性
Parent.call(this);
<script>
    function Parent() {  // 父类
        this.x = 100;
    }
    Parent.prototype.getX = function () { 
        return this.x;
    }
    function Child() {      // 子类
        // 1) 让this指向一个空对象
        // 2)this.xx = xx;
        // 3)返回这个空对象
        Parent.call(this);
        this.y = 200;
    }
    var c1 = new Child();
    console.log(c1.x);     //100
</script>

3、组合式继承:
1)Parent.call(this); // 继承父的私有属性
2)Child.prototype = Object.create(Parent.prototype);  子类继承父类的公有属性
<script>
    function Parent() {  // 父类
        this.x = 100;
    }
    Parent.prototype.getX = function () { 
        return this.x;
    }
    function Child() {      // 子类
        Parent.call(this); // 继承父的私有属性
        this.y = 200;
    }
    
    // 为了不影响父类的原型对象  copy一份赋值给了Child.prototype
    Child.prototype = Object.create(Parent.prototype);
    // 最好手动的修改一个Child原型对象上constructor指向
    Child.prototype.constructor = Child;  // 手动修改constructor的指向
    var c = new Child();
    console.log(c.x); 		 //100
    console.log(c.getX())  	//100
</script>
// Child.prototype = Parent.prototype;  不好
// 为什么?如果再你写了Child.prototype.xx = function(){}
// 意味着父中也可以访问xx   说白了 子类可以影响父类
// 把Parent.prototype对象copy一份  让它俩的原型是彼此独立
<script>
    var obj = {
        name:"wc",
        say(){
            console.log("this.name")
        }
    }
    obj.say()
    // Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__。
    let newObj = Object.create(obj);
    newObj.say();
    console.log(obj === newObj)
</script>

4、ES6中的继承:
super();   类似于前面我们所说的call继承  继承父的私有属性
extends    类似于原型对象 	继承公有属性
<script>
    class Parent{
        constructor() {
            this.x = 100;
        }
        getX(){
            return this.x;
        }
    }
    class Child extends Parent{
        constructor() {
            super(); // 类似于前面我们所说的call继承
            this.y = 200;
        }
        getY(){
            return this.y
        }
    }
    var child = new Child();
    console.log(child.x);  		//100
    console.log(child.getX()); 	//100
    console.log(child.y);		//200
    console.log(child.getY());	//200
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值