当前位置:网站首页>JS中原型及原型链
JS中原型及原型链
2022-07-27 05:03:00 【weixin_46051260】
1)原型
2)原型链:查找对象的属性和方法
1、对象本身找
2、构造函数找
3、对象原型找
4、构造函数原型找
5、向上原型中查找------》一直向上找,直到找不到 | 找到为止
原型链顶端是null,对象找不到属性是undefined
function Fun(){
this.test1=function(){
console.log('test1');
}
}
Fun.prototype.test2=function(){
console.log('test2');
}
var fun=new Fun()
fun.test1()//自己的属性
fun.test2()//原型链上找到的
console.log(fun.toString());//构造函数Fun的原型对象,也是object的一个实例化对象,
//顺着这条原型链能找到Object上的toString方法

3)JS中对原型的修改与重写
修改
function Person(name){
this.name=name
}
Person.prototype.getName=function(){
console.log(ths.name);
}
var p=new Person('张三')
console.log(p.__proto__===Person.prototype);//true
console.log(p.constructor);//ƒ Person(name){this.name = name}
console.log(p.__proto__===p.constructor.prototype);//true
重写
直接给Person的原型对象用对象进行赋值,p的构造函数指向根构造函数object
function Person(name){
this.name=name
}
//重写原型
Person.prototype={
getName:function(){
console.log(this.name);
}
}
var p=new Person('张三')
console.log(p.__proto__===Person.prototype);//true
console.log(p.__proto__);//{getName:f}
console.log(p.constructor.prototype);//{costructor:...}
console.log(p.__proto__===p.constructor.prototype);//false
p.constructor=Person
console.log(p.__proto__ === p.constructor.prototype);//true
题:
边栏推荐
- 后台优惠券管理
- Qsort - the sorting function in C language (with void*, callback function knowledge points
- How to get started quickly and strengthen learning?
- mysql 取消外键关联约束
- c语言字符串函数上:strlen、strcpy、strcat
- Li Hongyi machine learning team learning punch in activity day02 --- return
- JS中for...of和for...in的区别
- flask一对多数据库创建,基础增删改查
- Trying to evolve_ My first CSDN blog
- 程序环境和预处理(下):#define、#undef、命令行编译、条件编译、文件包含(超全整理,建议收藏!!!
猜你喜欢

上传七牛云的方法

DNSmasq使用总结

C WPF uses listbox to implement ruler control

函数和箭头函数

Multiplication sorting in torch, * & torch. Mul () & torch. MV () & torch. Mm () & torch. Dot () & @ & torch. Mutmal ()

用户登录-以及创建、验证短信验证码

pytorch安装新坑

elment-ui使用方法

Trying to evolve_ My first CSDN blog

First knowledge of C language -- what is C language
随机推荐
Xiaomi mall project_ register
C语言进制转换以及原补反码位运算介绍
Day5 --- Flask-RESTful请求响应与SQLAlchemy基础
GCC compilation options
The concept of cloud native application and 15 characteristics of cloud native application
初识C语言——什么是C语言
我的第一篇博客
Redis persistence
原生token生成加密、解密
C WPF uses listbox to implement ruler control
分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
Li Hongyi machine learning team learning punch in activity day03 --- error and gradient decline
页面布局中元素定位的几种方式
mysql 取消外键关联约束
node 安装调试
创建项目 实现登录注册,生成jwt,发送验证码
2021 Niuke multi school training camp 5 (question b)
图片上传的逻辑
Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)
Li Hongyi machine learning team learning punch in activity day01 --- introduction to machine learning