当前位置:网站首页>js原型和原型链以及原型继承
js原型和原型链以及原型继承
2022-08-10 02:56:00 【是小飞呀嘿】
目录
一、原型
原型是Javascript中的继承的基础,JavaScript的继承主要依靠原型链来实现的。
原型
在JavaScript中,我们创建一个函数A(就是声明一个函数), 就会为该函数创建一个prototype属性。而且也会在内存中创建一个对象B,A函数的属性 prototype 指向这个对象B( 即:prototype的属性的值是这个对象 )。这个对象B就是函数A的原型对象,简称函数的原型。
二、原型链
原型本身也是一个对象,所以它也会有一个自己的原型,这一层一层的延续下去,直到最后指向 null,这就形成的 原型链
// 原型和原型链
1、通过工厂函数创建对象
function createPerson(uname,age,phone){
let obj = new Object();
obj.uname = uname;
obj.age = age;
obj.phone = phone;
return obj;
}
let student0 = createPerson("aaa",20,"15156651651");
let student1 = createPerson("bbbb",20,"15156651651");
let student2 = createPerson("cccc",20,"15156651651");
let student3 = createPerson("ddd",20,"15156651651");
let student4 = createPerson("eeee",20,"15156651651");
console.log(student0,student1,student2,student3,student4);
function createDog(uname,age,type){
let dog = {
uname:uname,
age:age,
type:type
}
return dog;
}
let d0 = createDog("旺财",1,"中华田园犬");
let d1 = createDog("旺财12",1,"中华田园犬");
let d2 = createDog("旺财2",1,"中华田园犬");
let d3 = createDog("旺财321",1,"中华田园犬");
console.log(d0,d1,d2,d3);
// 2、通过[构造函数]实例化对象
function Person(uname,age,sex){ //构造函数
this.uname = uname;
this.age = age;
this.sex = sex;
}
let p = new Person("小飞",18,"男"); //实例化对象
let p1 = new Person("html",20,"nv ");
console.log(p,p1);
// this 指向当前调用他的最终的对象
/* 改变this指向
一、call()
1、立即调用并执行函数
2、可改变this指向
3、传递字符形式的参数
二、apply()
1、立即调用并执行函数
2、可改变this指向
3、只能传递数组的参数
三、bind()
1、不会调用并执行函数
2、可改变this指向
3、传递字符形式的参数
四、 new 关键字
1、在构造函数内部创建一个空对象
2、将 this 指向到当前这个空对象上
3、通过 this 向当前空对象上添加属性和方法
4、返回对象
*/
// 所有函数都有原型对象
// 构造函数访问原型对象 prototype --- 存放公共方法
// 实例对象访问原型对象 __proto__ --- 存放公共方法
// constructor 构造方法(原型对象创建的构造函数)
// 构造函数
function Person(uname,age,sex){
this.uname = uname;
this.age = age;
this.sex = sex;
// this.fn = function(){
// console.log(this.uname+"今年"+this.age+"岁了------"+this.sex);
// }
}
Person.prototype.fn = function(){
console.log("这是原型对象上的方法");
}
console.log("原型对象---- prototype",Person.prototype);
let p = new Person("小飞",18,"男"); //实例化对象
let p1 = new Person("html",20,"nv"); //实例化对象
let p2 = new Person("css",20,"nv"); //实例化对象
console.log(p,p1);
// p.fn();
// p1.fn();
//console.log(p.fn === p1.fn);
// console.log(Person.prototype === p.__proto__ );
// console.log(p1.fn === p.fn);
// console.log(Person.prototype.constructor === Person);
图示原型原型链解释:
蓝色为原型对象,简称原型
红色线连起来的为一条原型链
三、原型链继承
利用原型及原型链实现继承【面向对象】
//利用原型及原型链实现继承【面向对象】
//父 构造函数
function Father(name,age,sex){
// 指向创建它的实例对象
this.name = name;
this.age = age;
this.sex = sex;
}
// Father构造函数的原型对象上的公共方法
Father.prototype.money = function(){
//原型对象上的this 指向 调用它的 实例对象
console.log(this.name+"------在挣钱");
}
// 子 构造函数
function Son(name,age,sex,email){
// 指向创建它的实例对象 s
//使用 call() 立即调用父类方法 ,并传递参数 【继承父类属性】
Father.call(this,name,age,sex);
//子类 自身属性
this.email = email;
/* this.name = name;
this.age = age;
this.sex = sex; */
}
// 直接将父构造函数的原型对象赋值给子构造函数的原型对象
// Son.prototype = Father.prototype;
// 通过new 将父构造函数 的 实例对象 赋值给 son 的 原型对象
Son.prototype = new Father();
//利用 constructor 手动改变Son的原型对象的指向的构造函数
Son.prototype.constructor = Son;
// console.log("!!!",Son.prototype);
//在son 的 原型对象上添加 game 方法
Son.prototype.game = function(){
console.log(this.name + "-----在玩游戏");
}
let f = new Father("小飞",50,"男");
let s = new Son("html",20,"女","[email protected]");
console.log(f,s);
f.money();
s.money();
s.game();
边栏推荐
- 新零售社交电商APP系统平台如何打造公域+私域流量?
- Pen paper records
- Little rookie Hebei Unicom induction training essay
- program internationalization
- 有关视频传输时粘包问题的一些解决方法
- NFG电商系统在元宇宙趋势下做什么?
- 动态网页开发基础
- 快35了,还在“点点点”?那些入行几年的测试点工后来都怎么样了?
- flex 的 三个参数:flex-grow、flex-shrink、flex-basis
- Software life cycle (the work of each phase of software engineering)
猜你喜欢
(十四)时间延时任务及定时任务
How to quickly become a software test engineer?What skills do testers need for a monthly salary of 15k?
【图像分类】2022-CycleMLP ICLR
excel高级绘图技巧100讲(二十三)-Excel中实现倒计时计数
使用curl指令发起websocket请求
MySQL: What MySQL optimizations have you done?
程序国际化
想要避免After Effects渲染失败的问题,5个小技巧必看
Example 044: Matrix Addition
MySQL: Introduction to Logging System | Error Log | Query Log | Binary Log: Bin-log Data Recovery Practice | Slow Log Query
随机推荐
The so-called software testing ability is actually these 5 points
[Red Team] ATT&CK - Auto Start - Registry Run Key, Startup Folder
6 common plugin recommendations in Pycharm
【语义分割】2022-HRViT CVPR
...spread、命名空间、假报错、变化事件、async/await
zabbix添加监控主机和自定义监控项
Flink CDC 2.0及其他数据同步工具对比
元宇宙+NFT是“宝”还是“炒”
nodejs 时钟案例(fs模块),重复使用fs.writeFile方法,旧内容会被覆盖
2022/08/09 学习笔记 (day26) IO流
金融口译,口译中金融高频词有哪些
MySQL: Introduction to Logging System | Error Log | Query Log | Binary Log: Bin-log Data Recovery Practice | Slow Log Query
论文理解:“PIAT: Physics Informed Adversarial Training for Solving Partial Differential Equations“
Software life cycle (the work of each phase of software engineering)
PC摄像头设置 默认摄像头设置 win11 默认摄像头设置
怎么进行服务器性能监控,有什么监控工具
State compression small experience
flutter异步
【CC3200AI 实验教程5】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-定时器
Example 045: Summation