- js是基于原型、基于对象、解释型、弱类型语言。
- 它不是面向对象,通常用函数来拟对象。
- 通常有三种定义方式,而我们通常看到的json对象形式便是其中一种。`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>回顾js中如何定义对象</title>
</head>
<body>
</body>
<script type="text/javascript">
function Person(){}
var p1 = new Person()
p1.name="zhangsan";
p1.age="25";
p1.show=function(){
document.write(this.name+"---"+this.age+"<br/>");
}
p1.show();
document.write(p1.name+"<br/>");
document.write(p1["age"]+"<br/>");
function Person(name,age){
this.name = name;
this.age = age;
this.setName = function(name){
this.name = name;
}
this.setAge = function(age){
this.age = age;
}
this.getName = function(){
return this.name;
}
this.getAge = function(){
return this.age;
}
}
var p2 = new Person("zhangsan","25");
document.write(p2.getName()+"<br/>");
document.write(p2.name+"<br/>");
document.write(p2["age"]+"<br/>");
var p3 = {
"name":"zhangsan",
"age":"25",
"getName":function(){
return this.name;
}
}
document.write(p3.getName()+"<br/>");
document.write(p3.name+"<br/>");
document.write(p3["age"]);
</script>
</html>
![这里写图片描述](https://img-blog.csdn.net/2018082822342791?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lpY2hhX2k=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)