prototype原型链继承

每个函数上边都有一个原型 prototype

 // 我们 去创建一个构造函数
            function Person(){
                this.name = ' 这是世祖'
                this.age = '80'
                this.cart = 'BMW'
            }
            // 有一天 Person 嗝屁了 留下来了一些遗产,需要他的儿子去继承他的车和钱
            function PersonChildren(){

            }
            //继承
            PersonChildren.prototype  = new Person()
            //查看 person  的遗产 是否继承
            let personChildren = PersonChildren.prototype
            console.log(personChildren);
 	//输出结果:
            // Person {name: " 这是世祖", age: "80", cart: "BMW"}
            // age: "80"
            // cart: "BMW"
            // name: " 这是世祖"
            // __proto__:
            // constructor: ƒ Person()
            // arguments: null
            // caller: null
            // length: 0
            // name: "Person"
            // prototype: {constructor: ƒ}
            // __proto__: ƒ ()
            // [[FunctionLocation]]: index.html:29
            // [[Scopes]]: Scopes[2]
            // __proto__: Object
  • 可以看到person的儿子已经继承了他爸爸的遗产
  • 但是我们也可以看到 PersonChildren.prototype 的原型已经被覆盖掉了;
  • 我们可以使用一下方法,让他的原型重新指向他自己,这样他就有了自己的原型
PersonChildren.prototype.constructor=PersonChildren
console.log(PersonChildren.prototype);

  • 输出结果如下
  Person {name: " 这是世祖", age: "80", cart: "BMW", constructor: ƒ}
age: "80"
cart: "BMW"
constructor: ƒ PersonChildren()
arguments: null
caller: null
length: 0
name: "PersonChildren"
prototype: Person
age: "80"
cart: "BMW"
constructor: ƒ PersonChildren()
name: " 这是世祖"
__proto__: Object
__proto__: ƒ ()
[[FunctionLocation]]: index.html:38
[[Scopes]]: Scopes[2]
name: " 这是世祖"
__proto__: Object

总结:以此类推越继承越多 ,这就是原型链的的继承!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值