当前位置:网站首页>JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
2022-08-03 19:28:00 【HandsomeDanielWu】


prototype 只有在构造函数new()才有用。构造函数的prototype就是实例的原型。实例可以通过prototype上得到方法和属性,称为原型链查找。所以方法一般添加在prototype上,不直接添加在实例上
字面量










数组扩展方法 拓展类型的方法 prototype


内置构造函数的关系


——poto_是谷歌浏览器的特殊属性

Object是所有原型的终点,Object本身又可以看作Function new()出来的



继承











继承





解决继承问题1:借用构造函数 call apply





组合继承


组合继承

原型式继承 creat








原型式继承







寄生式继承








不同实例中,函数是不同的地址,造成资源浪费


寄生组合式




寄生

组合

不需要new 父类,相比组合式继承,减少1次构造函数调用。
<body>
<script>
//这个函数接受两个参数,subType子类的构造函数,supertype父类的构造函数
function inheritPrototype(subType, superType){
//寄生式继承
var prototype = Object.create(superType.prototype);
subType.prototype = prototype;
}
//父类
function People(name, sex, age){
this.name = name;
this.sex = sex;
this.age = age;
}
//如果方法不卸载prototype里,会导致创建出来的实例会有各自的方法函数地址,
//造成资源浪费
People.prototype.sayHello = function(){
console.log('你好,我是'+ this.name +'今年' + this.age +'岁了');
}
People.prototype.sleep = function(){
console.log(this.name + '正在睡觉');
}
//组合
//子类
function Student(name, sex, age, school, sid){
//构造函数,省去再写一遍父类属性的复制方法
//call解决子类实例的引用对象是相同内存地址问题
//this上下文,指student
People.call(this,name, sex, age);
//子类属性
this.school = school;
this.sid = sid;
}
//调用inheritPrototype函数,这个函数可以让Student类和prototype指向
//People.prototype为原型的一个新对象
inheritPrototype(Student,People);
Student.prototype.exam = function(){
console.log(this.name + '正在考试');
}
Student.prototype.sayHello = function(){
console.log(this.name + '学生 say hello');
}
var xiaoming = new Student('小明','男',12,'ABC学校',12345);
xiaoming.sayHello();
xiaoming.sleep();
xiaoming.exam();
</script>
</body> instanceof 检查是否在原型链上



边栏推荐
- Line the last time the JVM FullGC make didn't sleep all night, collapse
- 虚拟机vmware设置桥接模式上网
- net-snmp编译报错:/usr/bin/ld: cannot find crti.o: No such file or directory
- LeetCode 622. 设计循环队列
- 余弦距离介绍
- 盘点在线帮助中心对企业能够起到的作用
- 开源教育论坛| ChinaOSC
- 钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?
- LeetCode 952. 按公因数计算最大组件大小
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
猜你喜欢
随机推荐
net-snmp私有mib动态加载到snmpd
pg_memory_barrier_impl in Postgresql and C's volatile
MySQL master-slave, 6 minutes you master!
CS kill-free pose
Rust:多线程并发编程
APT级全面免杀与企业纵深防御体系的红蓝对抗
FreeRTOS Intermediate
Standard C language learning summary 11
LeetCode 622. 设计循环队列
Jingdong cloud released a new generation of distributed database StarDB 5.0
OneNote 教程,如何在 OneNote 中设置页面格式?
七夕之前,终于整出了带AI的美丽秘笈
基础软件与开发语言开源论坛| ChinaOSC
MySQL 主从,6 分钟带你掌握!
Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
网络协议-TCP、UDP区别及TCP三次握手、四次挥手
安装radondb mysql遇到问题
CS免杀姿势
Kettle 读取 Excel 数据输出到 Oracle 详解
力扣刷题之分数加减运算(每日一题7/27)








