当前位置:网站首页>原型的继承
原型的继承
2022-08-06 05:18:00 【西瓜味白桃汽水】
//原型链继承,问题会更改引用值的改变
function Super(){
this.a=[1,2,3,4]
}
Super.prototype.say=function(){
console.log(2222)
}
function Sub(){
}
Sub.prototype=new Super();
var sub1= new Sub();
var sub2=new Sub();
sub1.a.push(5)
//第二种方法,构造函数继承,没办法拿到原型上的方法
function Super(){
this.a=[1,2,3,4]
}
Super.prototype.say=function(){
console.log(2222)
}
function Sub(num){
Super.call(this,num)
}
var sub1= new Sub([1,2,3,4]);
var sub2=new Sub();
console.log(sub1)
//组合继承,使用原型链继承一次再用构造函数继承一次
function Super(){
this.a=[1,2,3,4]
}
Super.prototype.say=function(){
console.log(2222)
}
function Sub(){
Super.call(this)
}
Sub.prototype=new Super()
var sub1= new Sub();
var sub2=new Sub();
console.log(sub1.b)
//寄生组合继承
function Super(){
this.a=[1,2,3,4]
}
Super.prototype.say=function(){
console.log(2222)
}
function Sub(){
Super.call(this)
}
if(!Object.create){
//解决兼容
Object.create=function(proto){
function F(){
F.prototype=proto
return new F();
}
}
}
Sub.prototype=Object.create(Super.prototype)
var sub1= new Sub();
var sub2=new Sub();
console.log(sub1.b)
边栏推荐
猜你喜欢

ES6之面对对象 class类

基于flask框架web代码pyinstaller打包

Responsive dream weaving template wire mesh website

ACC编程应用挑战赛决赛真题

matlab 离散无记忆信源建模

process

Metal material/polypeptide/polysaccharide/compound/antibody/quantum dot/black phosphorus quantum dot modified hydrogel

工业机器人复习题

环境敏感性/温核磁共振/载生长素/载细胞膜/载蛋白质水凝胶的研究

互联网时代下服务器该如何进行主机加固
随机推荐
flinkcdc mysql DataStream API问题错在哪?
Oracle Recovery Tools to solve ORA-600 3020 failure
ES6之面对对象 class类
1403. 非递增顺序的最小子序列
小程序学习笔记
【三子棋实例】C语言实现三子棋游戏
el-table filter data
What are the application scenarios of LoRa technology?
路由-route
Flutter performance optimization TAB, switch back without rendering again
aws篇8
How to avoid the account risk of cross-border e-commerce risk?What is the solution?
公私钥保存成PEM文件
1007:计算(a+b)×c的值
Comprehensive review materials for "Public Relations"
idea 常用快捷键
Responsive dream weaving template wire mesh website
Using superagent in TypeScript
ES6迭代器与生成器
fegin feign.FeignException: status 404 reading StudenService#getAll()