当前位置:网站首页>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
边栏推荐
- 可以接收多种数据类型参数——可变参数
- C introduction of variable parameter params
- 2022年度Top9的任务管理系统
- Knowledge of software testing~
- 2022A特种设备相关管理(电梯)上岗证题库及模拟考试
- What kind of experience is it to prepare for a month to participate in ACM?
- Web Course Design - his system
- Use of metagroup object tuple in C
- JSON related
- ASP. Net and ASP NETCORE multi environment configuration comparison
猜你喜欢

Experiment 5 components and event handling

荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比

编码电机PID调试(速度环|位置环|跟随)

由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。

《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案

Maui initial experience: Cool

Comprehensive calculation of employee information

【无标题】

C read / write binary file

关于idea调试模式下启动特别慢的优化
随机推荐
Docker拉取mysql并连接
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
svg标签中利用<polygon/>循环数组绘制多边形
xutils3修改了我提报的一个bug,开心
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
If the deep replication of objects is realized through C #?
Blazor University (11) component - replace attributes of subcomponents
超好用的Excel异步导出功能
2022年P气瓶充装培训试题及模拟考试
C WPF UI framework mahapps switching theme
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
C read / write binary file
oracle 查询外键含有逗号分隔的数据
C introduction of variable parameter params
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Knowledge of software testing~
Fiddler use
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
Peut recevoir plusieurs paramètres de type de données - paramètres variables
ASP. Net and ASP NETCORE multi environment configuration comparison