第五天:内置对象(7.Javascript内置对象)

1)中所术是内置对象,2)中为自定义对象

代码说明如下

2.1.1 定义并创建对象实例方式1,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
people= new Object();
people.name="iwen";
people.age="30";
document.write("name:"+people.name+";age:"+people.age);
</script>
</body>
</html>

运行结果:

2.1.2 定义并创建对象实例方式2,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
people={name:"lucy",age:"32"};
document.write("name:"+people.name+";age:"+people.age);
</script>
</body>
</html>

运行结果:

2.2 使用函数来创建对象,然后创建新的对象实例

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
function people(name,age){
this._name=name;
this._age=age;
}
son=new people("LiLei","10");
document.write("name:"+son._name+";age:"+son._age);

</script>
</body>
</html>

运行结果:

犯过的错误:

son = new people("lily",20);

写成了 son = people("lily",20);

原文地址:https://www.cnblogs.com/fenr9/p/5621480.html