当前位置:网站首页>js原型陷阱
js原型陷阱
2022-07-01 09:13:00 【su27_0101】
js原型属性具有实时性
何为实时性
如:
function Person(){
this.name = "mike";
}
var p1 = new Person();
p1.name; // mike
Person.prototype.age = 12;
p1.age; // 12 实时挂在新属性
不靠谱的constructor
function Person(){
this.name = "mike";
}
Person.prototype.say = function(){
return this.name;
}
var p1 = new Person();
p1.say(); // "mike"
p1.constructor;
/*
ƒ Person(){
this.name = "mike";
}
*/
Person.prototype = {
sex: 'male'
}
p1.say();//"mike"
var p2 = new Person();
p2.say();// error;
p2.constructor; // ƒ Object() { [native code] }
改变了Person.prototype constructor 也发生变化
但旧的p1 通过 __proto__ 任然可以访问到 say
如何避免constructor丢失
在改变prototype后请将constructor指回原来的constructor
function Person(){}
Person.prototype = {};
Person.prototype.constructor = Person;
边栏推荐
- nacos簡易實現負載均衡
- Serialization, listening, custom annotation
- Shell脚本-变量的定义、赋值和删除
- 序列化、监听、自定义注解
- [pytorch] 2.4 convolution function nn conv2d
- How to manage fixed assets well? Easy to point and move to provide intelligent solutions
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
- JCL and slf4j
- TV size and viewing distance
- FreeRTOS learning easy notes
猜你喜欢

【pytorch】nn. Crossentropyloss() and nn NLLLoss()

Redis -- lattice connects to redis cluster

Mysql 优化

小鸟识别APP

NoSQL数据库的安装和使用

Structure de l'arbre - - - arbre binaire 2 traversée non récursive

树结构---二叉树2非递归遍历

Pain points and solutions of fixed assets management of group companies

Personal decoration notes

Embedded Engineer Interview Question 3 Hardware
随机推荐
Shell脚本-case in 和正则表达式
3D printing Arduino four axis aircraft
Shell script -read command: read data entered from the keyboard
Shell脚本-while循环详解
美团2022年机试
【pytorch】nn.AdaptiveMaxPool2d
如何高效拉齐团队认知
NiO zero copy
Flink interview questions
Ape anthropology topic 20 (the topic will be updated from time to time)
Mysql 优化
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
2.4 激活函数
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
Simple load balancing with Nacos
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
Mysql8.0 learning record 17 -create table
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
记一次redis超时