JS中工厂函数

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
		//工厂函数是专门创建对象的函数
		function createPersion(myname,myage)
		{
			let obj=new Object();
			obj.name=myname;
			obj.age=myage;
			obj.say=function()
			{
				console.log("hello world");
			}
			return obj;
		}
		let obj1=createPersion("cyg",22);
		let obj2=createPersion("liwen",20);
		console.log(obj1);
		console.log(obj2);
	</script>
</body>
</html>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页