当前位置:网站首页>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
题:
边栏推荐
- [C language switch branch statement and loop statement]
- 后台品牌管理功能实现
- 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)
- flask一对多数据库创建,基础增删改查
- 后台优惠券管理
- 我的第一篇博客
- 注册功能实现
- Program environment and preprocessing (Part 1): how does a program run successfully?
- p7 day1 初识Flask框架
- 订单系统功能实现
猜你喜欢

分享一道关于程序编译过程的选择题(内含编译过程浅谈,符号表的形成合并过程)

Share a multiple-choice question about variables (including global variables, local variables, the scope of variables, and life cycle knowledge points)

强制登录,七牛云上传图片

JDBC API details

Pytorch data type and numpy data are mutually transformed

node 安装调试

如何将Excel表格中的多列内容合并到一列

创建项目 实现登录注册,生成jwt,发送验证码

初识C语言——什么是C语言

Hi3516dv300 environment setup
随机推荐
cmd命令和npm命令
流程控制-分支
GCC 编译选项
Introduction to C language functions
分享力扣—189.轮转数组 的三种解法
Collation of several difficult methods in pytorch --gather & squeeze & unsqueeze
Li Hongyi machine learning team learning punch in activity day04 - Introduction to deep learning and back propagation mechanism
【codeforces round#800 B. Paranoid String】DP
【codeforces 1695C Zero Path】DP
努力进化中_我的第一篇csdn博客
弹性盒/伸缩盒(flex)的使用
进制的特性
后台品牌管理功能实现
JWT认证及登录功能实现,退出登录
分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
C语言入门介绍
while循环
Flask登录实现
C语言初阶——分支语句(if,switch)
C language string function: strlen, strcpy, strcat