当前位置:网站首页>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;
边栏推荐
- Promise异步编程
- 【pytorch】nn. Crossentropyloss() and nn NLLLoss()
- Shell script - string
- Can diffusion models be regarded as an autoencoder?
- Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
- Is it safe to dig up money and make new shares
- Principles of Microcomputer - internal and external structure of microprocessor
- An overview of the design of royalties and service fees of mainstream NFT market platforms
- Shell script - special variables: shell $, $*, [email protected], $$$
- Personal decoration notes
猜你喜欢
Installation and use of NoSQL database
足球篮球体育比赛比分直播平台源码/app开发建设项目
Bird recognition app
3D打印Arduino 四轴飞行器
How to realize the usage of connecting multiple databases in idel
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
Installing Oracle EE
PR training notes
Redis——Lettuce连接redis集群
Design and manufacture of simple digital display electronic scale
随机推荐
[video game training] real topic of 2013 video game of infrared optical communication device
The fixed assets management system enables enterprises to dynamically master assets
[pytorch] softmax function
Is it safe to dig up money and make new shares
Ape anthropology topic 20 (the topic will be updated from time to time)
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
Set the type of the input tag to number, and remove the up and down arrows
记一次redis超时
Shell脚本-字符串
Learning practice: comprehensive application of cycle and branch structure (II)
Can diffusion models be regarded as an autoencoder?
Log4j 日志框架
Input标签的type设置为number,去掉上下箭头
如何高效拉齐团队认知
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
Meituan machine test in 2022
Promise asynchronous programming
【pytorch】nn. AdaptiveMaxPool2d
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
Promise异步编程