当前位置:网站首页>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;
边栏推荐
- Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
- Shell脚本-字符串
- Shell脚本-变量的定义、赋值和删除
- Key points of NFT supervision and overseas policies
- Tree structure --- binary tree 1
- dsPIC30F6014a LCD 方块显示
- 毕业季,我想对你说
- Is it safe to dig up money and make new shares
- 2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
- Jetson nano installs tensorflow GPU and problem solving
猜你喜欢

Tree structure -- binary tree 2 non recursive traversal

【电赛训练】红外光通信装置 2013年电赛真题

Why is the Ltd independent station a Web3.0 website!

【pytorch】softmax函数

3D printing Arduino four axis aircraft

猿人学第20题(题目会不定时更新)

Installing Oracle EE

Principles of Microcomputer - internal and external structure of microprocessor

dsPIC30F6014a LCD 方块显示

Imitation of Baidu search results top navigation bar effect
随机推荐
Daily practice of C language - day 80: currency change
JCL 和 SLF4J
Databinding source code analysis
How to solve the problem of fixed assets management and inventory?
小鸟识别APP
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
Summary of reptile knowledge points
JCL and slf4j
【pytorch】nn. AdaptiveMaxPool2d
Common interview questions for embedded engineers 2-mcu_ STM32
pcl_viewer命令
记一次redis超时
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
Flink interview questions
[pytorch learning] torch device
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
易点易动助力企业设备高效管理,提升设备利用率
Meituan machine test in 2022
Preparing for the Blue Bridge Cup -- bit operation
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)