当前位置:网站首页>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 检查是否在原型链上
边栏推荐
猜你喜欢
入门3D建模基础教程详细分解
【木马免杀】
基础软件与开发语言开源论坛| ChinaOSC
Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
ECCV2022 | 用于视频问题回答的视频图Transformer
微信小程序分享功能
LeetCode 952. 按公因数计算最大组件大小
LeetCode 952. Calculate Maximum Component Size by Common Factor
【夜莺监控方案】08-监控msyql集群(prometheuse+n9e+mysqld_exporter)
Introduction to Cosine Distance
随机推荐
力扣刷题之有效的正方形(每日一题7/29)
国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA
Postgresql源码(65)新快照体系Globalvis工作原理分析
京东云发布新一代分布式数据库StarDB 5.0
Web项目中简单使用线程池
LeetCode 952. Calculate Maximum Component Size by Common Factor
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
epoll + 线程池 + 前后置服务器分离
力扣刷题之合并两个有序数组
redis常用命令,HSET,XADD,XREAD,DEL等
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
Don't look down upon the WebSocket!Long connection, stateful, two-way, full-duplex king is Fried
go语言实现导出string字符串到文件中
【微信小程序】NFC 标签打开小程序
Interview Blitz: What Are Sticky Packs and Half Packs?How to deal with it?
ERROR: You don‘t have the SNMP perl module installed.
基于移动GIS的环保生态管理系统
丙二醇二乙酸酯(Propylene Glycol Diacetate)
MySQL详细学习教程(建议收藏)
net-snmp私有mib动态加载到snmpd