当前位置:网站首页>new的实现
new的实现
2022-04-23 06:26:00 【笔描相思】
1:首先会创建一个对象
2:创建构造函数上面的属性和方法给这个对象
3:将这个对象的__proto__的属性赋一个构造函数的protypeof
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
</title>
<script>
// alert(2423)
// console.log("423");
function Person(name,age)
{
this.name=name;
this.age=age;
}
function _New()
{
//创建一个对象
const obj={};
const [Person1,...arg]=[...arguments];
//给对象创建构造函数中的属性和方法
var res=Person1.apply(obj,arg);
//实例的显示原型指向构造函数的显示原型
obj.__proto__=Person1.prototype;
//如果构造函数自己有返回值,并且是对象的话我们就使用这个对象,否则就返回刚刚创建的对象
return res==='Object'?res:obj;
}
console.log(_New(Person,'小明',15));
</script>
</head>
<body>
</body>
</html>
版权声明
本文为[笔描相思]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44788119/article/details/120902190
边栏推荐
猜你喜欢

Solutions to common problems in visualization (VII) solutions to drawing scale setting

数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析

自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)

Reflection on the systematic design of Android audio and video caching mechanism

Discussion on arrow function of ES6

js之预解析
![[Educational Codeforces Round 80] 解题报告](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[Educational Codeforces Round 80] 解题报告

How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)

MySQL index

SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
随机推荐
[self motivation series] you'll never be ready
斐波拉去动态规划
keytool: command not found
js之DOM学习获取元素
The difference and application of VR, AR and MR, as well as some implementation principles of AR technology
【自我激励系列】你永远不会准备好
npm 安装踩坑
[CodeForces - 208E] Blood Cousins(k代兄弟问题)
11.表和库的管理
判断字符串首尾是否包含目标参数:startsWith()、endsWith()方法
配置npm
技能点挖坑
Mysql 数据库从设计上的优化
js之预解析
2022.3.14 Ali written examination
Reflection on the systematic design of Android audio and video caching mechanism
redis连接出错 ERR AUTH <password> called without any password configured for the default user.
Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)
二叉树的深度
MySQL isolation level