1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title></title>
6 </head>
7 <body>
8
9 </body>
10 </html>
11 <script>
12 //对象构造函数
13 // 私有属性好处: 安全 就类似闭包中的函数一样 减少污染
14 function Person(name) {
15 //私有属性,只能在对象构造函数内部使用
16 var className = "用户对象";
17 //公有属性,在对象实例化后调用
18 this.name = name;
19 //私有方法
20 var privateFunction = function () {
21 alert(this.name);
22 }
23 //公有方法
24 this.publicFunction = function () {
25 alert(this.name); //公有属性
26 alert(className); //正确 直接通过变量名访问
27 alert(this.className); //undefined 错误 不能这样访问
28 }
29 //公有属性
30 alert(className);
31 //正确 直接通过变量名访问
32 alert(this.className); //undefined 错误 不能这样访问
33 }
34
35 /*什么是公有属性:*/
36 /*使用象的人可以访问到对象内部的某个属性*/
37 var person = new Person('xiaowang')
38
39 console.log(person.className);
40 console.log(person.name)
41 console.log(person.publicFunction())
42 console.log(person.privateFunction())
43
44 </script>