当前位置:网站首页>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;
边栏推荐
- An overview of the design of royalties and service fees of mainstream NFT market platforms
- Graduation season, I want to tell you
- 易点易动助力企业设备高效管理,提升设备利用率
- Input标签的type设置为number,去掉上下箭头
- Shell脚本-read命令:读取从键盘输入的数据
- 序列化、监听、自定义注解
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- 【pytorch】2.4 卷积函数 nn.conv2d
- How to realize the usage of connecting multiple databases in idel
- [ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
猜你喜欢

Daily practice of C language - day 80: currency change
![[pytorch] softmax function](/img/97/b8ae22e8496a77e665d716cb0e9ee3.png)
[pytorch] softmax function

樹結構---二叉樹2非遞歸遍曆

Structure de l'arbre - - - arbre binaire 2 traversée non récursive

NiO zero copy

2.2 【pytorch】torchvision.transforms

Principle and application of single chip microcomputer timer, serial communication and interrupt system

Understanding and implementation of AVL tree

How to manage fixed assets efficiently in one stop?

Imitation of Baidu search results top navigation bar effect
随机推荐
The fixed assets management system enables enterprises to dynamically master assets
2.4 激活函数
类加载
I use flask to write the website "one"
MySQL optimization
Shell script -select in loop
【pytorch】nn. AdaptiveMaxPool2d
Preparing for the Blue Bridge Cup -- bit operation
Microcomputer principle - bus and its formation
How to solve the problem of fixed assets management and inventory?
It technology ebook collection
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
Flink面试题
Understanding and implementation of AVL tree
【pytorch】2.4 卷积函数 nn.conv2d
小鸟识别APP
Tree structure -- binary tree 2 non recursive traversal
Design and manufacture of simple digital display electronic scale
Shell script case in and regular expressions
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things