javascript对象实例化

时间:2022-05-02 09:23:31
代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body>
<script>
// 方法一 通过函数定义一个对象 
function person(firstname,lastname,age )
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
 }
//实例化一个对象 myFather
var myFather=new person("Steve","Jobs",48 );
document.write(myFather.lastname);
//方式二 直接通过new Object()
person2=new Object();
person2.name = "p2"
document.write(person2.name);
</script>
hello,world
</body>
</html>