当前位置:网站首页>JS implementation of new
JS implementation of new
2022-04-23 03:15:00 【GeXueliu】
Realization new The operator
- Create a blank object
- Modify the prototype of the blank object to the prototype of the constructor
- this Point to a blank object , And execute the constructor
- Returns the object
function Animal(name,age) {
this.name = name;
this.age = age;
}
function _new(constructor,...args) {
let instance = {
};
// instance._proto_ = constructor.prototype; Are four underscores
instance.__proto__ = constructor.prototype;
let result = constructor.call(instance,...args);
return typeof result === 'object'? result:instance; // Returns the non empty object returned by the constructor , Otherwise return the created object
}
let a = _new(Animal,'lli','2');
console.log('a',a)
console.log(a instanceof Animal);
// expected output: true
版权声明
本文为[GeXueliu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627323115.html
边栏推荐
- 一文了解全面静态代码分析
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- [new version release] componentone added Net 6 and blazor platform control support
- What kind of experience is it to prepare for a month to participate in ACM?
- Test experience data
- Using positive and negative traversal to solve the problem of "the shortest distance of characters"
- Is it difficult to choose binary version control tools? After reading this article, you will find the answer
- 二进制文件版本控制工具选择难?看完这篇你会找到答案
- ASP. Net and ASP NETCORE multi environment configuration comparison
- Docker拉取mysql并连接
猜你喜欢
![[MySQL] left Function | Right Function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left Function | Right Function

2022G2电站锅炉司炉考试题库及在线模拟考试

The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details

C read / write binary file

Drawing polygons with < polygon / > circular array in SVG tag

Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful

软件测试相关知识~
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

可以接收多种数据类型参数——可变参数

可以接收多種數據類型參數——可變參數
随机推荐
編碼電機PID調試(速度環|比特置環|跟隨)
Use split to solve the "most common words" problem
Huawei mobile ADB devices connection device is empty
一套组合拳,打造一款 IDEA 护眼方案
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
2022t elevator repair test simulation 100 questions and online simulation test
Eight elder brothers chronicle [4]
C语言实现通讯录----(静态版本)
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
OLED多级菜单记录
Peut recevoir plusieurs paramètres de type de données - paramètres variables
Load view Caton
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
Flink实时数仓项目—DWS层设计与实现
ThreadLocal 测试多线程变量实例
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
ASP. Net 6 middleware series - conditional Middleware
手机连接电脑后,QT的QDIR怎么读取手机文件路径