当前位置:网站首页>js 实现new
js 实现new
2022-04-22 06:29:00 【GeXueliu】
实现 new 操作符
- 创建空白对象
- 修改空白对象的原型为构造函数的原型
- this 指向空白对象,并执行构造函数
- 返回对象
function Animal(name,age) {
this.name = name;
this.age = age;
}
function _new(constructor,...args) {
let instance = {
};
// instance._proto_ = constructor.prototype; 是四个下划线
instance.__proto__ = constructor.prototype;
let result = constructor.call(instance,...args);
return typeof result === 'object'? result:instance; // 返回构造函数返回的非空对象,否则返回创建的对象
}
let a = _new(Animal,'lli','2');
console.log('a',a)
console.log(a instanceof Animal);
// expected output: true
版权声明
本文为[GeXueliu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/GeXueliu/article/details/124153035
边栏推荐
- SuperSocket在.Net5中使用——Command篇
- Read SAE J1939 protocol data stream
- [GPS - NMEA-0183 protocol]
- 2021-10-08 selenium headless mode open browser
- 文件安全笔记
- SuperSocket在.Net5中使用——WebSocket Server篇
- 树莓派4B:使用raspi-config实现USB BOOT
- 【从零开始写漏扫】主机发现——手写一个子域名挖掘器
- Supersocket is Use in net5 - websocket server
- 行业应用 |从实践中读懂银行的敏感数据安全防护思路
猜你喜欢

Xshell connect virtual machine

OBD external test equipment initialization judgment protocol type (SAE J1939 / iso15765 / iso27145)

文件安全笔记

行业应用 | 政务大数据平台数据安全合规建设之道

Read iso15031 protocol data stream

About XSS Cross Station

关于信息收集

Practical application of callable combined with countdownlatch

Raspberry Pie: adafruit's I2C ssd1306 OLED driver

世平信息参与衡阳市“船山论坛”企业路演,畅谈数据安全,助力协同创新
随机推荐
MySQL queries the attributes of all fields in the database
[TCP / IP V ICMP]
[TCP / IP four IP internet protocol]
Redis listens for key expiration events
2021-10-08 selenium headless mode open browser
简单c语言练习:学生数据的存取
落实数据合规,保障数据安全
Supersocket is Use in net5 - websocket server
Bert模型系列一:输入到底是什么东西呢?
SuperSocket在.Net5中使用——Command篇
[C - attribute]
行业应用 | 政务大数据平台数据安全合规建设之道
文件安全笔记
在window环境下面virtualbox加载移动硬盘里创建好的虚拟机
基于卷积神经网络LeNet-5模型的mnist手写数字识别
Raspberry Pie 4: custom network time source
C-10 maximum common divisor and minimum common multiple
Autoware displays user interface details
Raspberry Pie: 4.2-inch ink screen
聚焦科技创新 | 世平信息亮相科创中国技术路演——信息技术与安全专场