JavaScript创建对象
1、实现源码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript创建对象</title>
<script type="text/javascript">
/**
* 创建Person对象
*/
function Person(name,sex,age,addr)
{
this.name = name;
this.sex = sex;
this.age = age;
this.date = obDate();
this.addr = addr;
this.findInfo = function(){
name = "姓名:" + name + "<br>";
sex = "性别:" + sex + "<br>";
age = "年龄:" + age + "<br>";
date = "生日:" + obDate() + "<br>";
addr = "地址:" + addr;
document.writeln(name,sex,age,date,addr);
};
}
/**
* 获取现在日期
*/
function obDate()
{
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var thisDate = year + "-" + (month<10?"0"+month:month) + "-" + (day<10?"0"+day:day);
return thisDate;
}
You = new Person("You","男","23","湖北省武汉市");
You.findInfo();
</script>
</head>
<body>
</body>
</html>
2、实现结果
姓名:You
性别:男
年龄:23
生日:2016-04-29
地址:湖北省武汉市