当前位置:网站首页>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;
边栏推荐
- dsPIC30F6014a LCD 方块显示
- 小鸟识别APP
- The fixed assets management system enables enterprises to dynamically master assets
- Flink面试题
- Set the type of the input tag to number, and remove the up and down arrows
- SDN_ Simple summary
- Jetson Nano 安装TensorFlow GPU及问题解决
- Principles of Microcomputer - Introduction
- Shell script -while loop explanation
- Naoqi robot summary 28
猜你喜欢
Simple load balancing with Nacos
OSPF - virtual link details (including configuration commands)
An overview of the design of royalties and service fees of mainstream NFT market platforms
Mysql 优化
jeecg 重启报40001
[pytorch] softmax function
TV size and viewing distance
2.4 activation function
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
2.4 激活函数
随机推荐
Class loading
Shell脚本-变量的定义、赋值和删除
SDN_ Simple summary
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
易点易动助力企业设备高效管理,提升设备利用率
Differences among tasks, threads and processes
Shell script - array definition and getting array elements
Flink面试题
Naoqi robot summary 28
【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
树结构---二叉树2非递归遍历
Shell脚本-case in语句
Shell script case in statement
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
JCL 和 SLF4J
【pytorch】softmax函数
Can diffusion models be regarded as an autoencoder?
Shell script - definition, assignment and deletion of variables
How to manage fixed assets efficiently in one stop?
JCL and slf4j