当前位置:网站首页>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;
边栏推荐
- [pytorch] softmax function
- How to launch circle of friends marketing and wechat group activities
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- 序列化、监听、自定义注解
- Flink interview questions
- 【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- Daily office consumables management solution
- Serialization, listening, custom annotation
- FreeRTOS learning easy notes
猜你喜欢
Why is the Ltd independent station a Web3.0 website!
【pytorch】softmax函数
Principles of Microcomputer - Introduction
足球篮球体育比赛比分直播平台源码/app开发建设项目
【电赛训练】红外光通信装置 2013年电赛真题
NiO zero copy
Vsync+ triple cache mechanism +choreographer
Phishing identification app
Implementation and application of queue
【检测技术课案】简易数显电子秤的设计与制作
随机推荐
3D printing Arduino four axis aircraft
Shell script -for loop and for int loop
美团2022年机试
Why is the Ltd independent station a Web3.0 website!
Mysql 优化
Football and basketball game score live broadcast platform source code /app development and construction project
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Shell script - string
The fixed assets management system enables enterprises to dynamically master assets
OSPF - virtual link details (including configuration commands)
Shell script -if else statement
Jetson nano installs tensorflow GPU and problem solving
Daily practice of C language - day 80: currency change
Shell script -while loop explanation
Principles of Microcomputer - internal and external structure of microprocessor
Redis——Lettuce连接redis集群
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
Class loading
【pytorch】nn.AdaptiveMaxPool2d
钓鱼识别app