当前位置:网站首页>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 service configuration and persistence configuration
- Shell脚本-字符串
- 序列化、监听、自定义注解
- SDN_简单总结
- Common interview questions for embedded engineers 2-mcu_ STM32
- Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
- Personal decoration notes
- 2.4 激活函数
- Shell脚本-while循环详解
- [ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
猜你喜欢
随机推荐
[pytorch] softmax function
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Leetcode daily question brushing record --540 A single element in an ordered array
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
The fixed assets management system enables enterprises to dynamically master assets
Class loading
In the middle of the year, where should fixed asset management go?
Daily practice of C language - day 80: currency change
如何高效拉齐团队认知
Shell script -read command: read data entered from the keyboard
Can diffusion models be regarded as an autoencoder?
樹結構---二叉樹2非遞歸遍曆
Common interview questions for embedded engineers 2-mcu_ STM32
Summary of reptile knowledge points
MapReduce编程基础
树结构---二叉树1
Shell脚本-数组定义以及获取数组元素
How to solve the problem of fixed assets management and inventory?
Flink面试题
PR training notes





![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
![[pytorch] softmax function](/img/97/b8ae22e8496a77e665d716cb0e9ee3.png)


