js原型链继承的关系

254次阅读
没有评论

js原型链继承的关系

1、构造函数有原型对象,原型对象有指针指向结构函数,每个实例都有内部指针指向原型对象。

2、Father通过new给Children的原型对象赋值一个实例,从而实现Children继承Father。

实例

// 父构造函数
function Father() {
    this.name = "father"
    this.house = "cottage"
}
// 原型方法
Father.prototype.alertName = function () {
    console.log(this.name)
}
// 创造实例
let f = new Father()
 
f.alertName()//father
 
// 子构造函数
function Children() {
    this.name = "children"
}
// 实现继承:子构造函数的原型对象=父构造函数的实例对象
Children.prototype = new Father()
// 创建子实例
let c = new Children()
// 儿子就继承了父亲的所有属性(大别墅),并且获得了自己的名字
 
c.alertName()//children
 
console.log(c.house)//cottage

以上就是js原型链继承的关系,希望对大家有所帮助。更多js学习指路:js教程

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:JavaScript2022-12-15发表,共计617字。
新手QQ群:570568346,欢迎进群讨论 Python51学习