您的位置:首页 > Web前端 > JavaScript

在Javascript中定义对象类别

2006-12-22 00:00 465 查看
From: JavaEye.com

注意JavaScript中对象类别的定义,使用function来定义对象类别,初始化对象使用new操作符
function Person(name, age) {
 this.name = name;
 this.age = age;
 this.toString = function() {
 document.writeln("[name]:"+this.name+"<br>"+"[age]:"+this.age);
 }
}

完整的简单HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JavaScript</title>
<script language="javascript" type="text/javascript">
function Person(name, age) {
 this.name = name;
 this.age = age;
 this.toString = function() {
 document.writeln("[name]:"+this.name+"<br>"+"[age]:"+this.age);
 }
}
</script>
</head>

<body>
<script type="text/javascript">
 var person = new Person();
 person.name="robbin";
 person.age=30;
 person.toString();
</script>
</body>
</html>

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息