可以根据需要创建一个对象,以下是一个示例:

<!DOCTYPE html>
<html>
<head>
	<title>创建对象示例</title>
</head>
<body>
	<script>
		// 创建一个对象
		var person = {
			firstName: "John",
			lastName: "Doe",
			age: 30,
			eyeColor: "blue",
			fullName: function() {
				return this.firstName + " " + this.lastName;
			}
		};

		// 输出对象属性
		document.write("First Name: " + person.firstName + "<br>");
		document.write("Last Name: " + person.lastName + "<br>");
		document.write("Age: " + person.age + "<br>");
		document.write("Eye Color: " + person.eyeColor + "<br>");
		document.write("Full Name: " + person.fullName() + "<br>");
	</script>
</body>
</html>

在上面的示例中,我们创建了一个名为person的对象,它有4个属性:firstNamelastNameageeyeColor,以及一个方法fullName(),该方法返回完整的姓名。

我们使用document.write()输出了对象的属性,以及调用了fullName()方法。

创建一个对象请生成js源码并存放在script中

原文地址: http://www.cveoy.top/t/topic/9zW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录