当前位置:网站首页>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
边栏推荐
- Preview of converting doc and PDF to SWF file
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- First in the binary tree
- PID debugging of coding motor (speed loop | position loop | follow)
- 准备一个月去参加ACM,是一种什么体验?
- Knowledge of software testing~
- Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
- Web Course Design - his system
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
- Xamarin effect Chapter 21 expandable floating operation button in GIS
猜你喜欢
![[Mysql] LEFT函数 | RIGHT函数](/img/26/82e0f2280de011636c26931a74e749.png)
[Mysql] LEFT函数 | RIGHT函数

Yes Redis using distributed cache in NE6 webapi

The most easy to understand service container and scope of dependency injection

After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path

be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?

This new feature of C 11, I would like to call it the strongest!

A comprehensive understanding of static code analysis

手机连接电脑后,QT的QDIR怎么读取手机文件路径

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

It can receive multiple data type parameters - variable parameters
随机推荐
Establishing and traversing binary tree
ThreadLocal 测试多线程变量实例
General test technology [II] test method
超好用的【通用Excel导入功能】
ASP. Net 6 middleware series - execution sequence
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
Two methods are used to solve the "maximum palindrome product" problem
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
Using stack to solve the problem of "mini parser"
The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
Fight leetcode again (290. Word law)
Ide-idea-problem
OLED多级菜单记录
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
Find the number of leaf nodes of binary tree
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
oracle 查询外键含有逗号分隔的数据
Use of metagroup object tuple in C
If the deep replication of objects is realized through C #?