当前位置:网站首页>Implementation of new

Implementation of new

2022-04-23 08:05:00 Pen drawing Acacia

1: First, an object will be created
2: Create the properties and methods above the constructor to this object
3: Put this object's __proto__ Property of a constructor 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()
			{
			    // Create an object 
			    const obj={};
				const [Person1,...arg]=[...arguments];
			    // Create properties and methods in the constructor for the object 
			    var res=Person1.apply(obj,arg);
			    // The display prototype of the instance points to the display prototype of the constructor 
			    obj.__proto__=Person1.prototype;
			    // If the constructor has its own return value , And if it's an object, we'll use this object , Otherwise, it returns the object just created 
			    return res==='Object'?res:obj;
			}
			
			console.log(_New(Person,' Xiao Ming ',15));
		</script>
	</head>
	<body>
	</body>
</html>

版权声明
本文为[Pen drawing Acacia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624332845.html