<script type="text/javascript"> function test(){ var obj = this; obj.pub1 = ‘pub1‘; //公有变量 var pri1 = ‘pri1‘; //私有变量(类内部访问相当于private修饰) this.getPri1 = function(){ //实现一个访问器使外部能访问到是有变量 return pri1; } } var service = new test(); console.log(service.pub1); // pub1 console.log(service.pri1); // undefined(无法访问到私有变量) console.log(service.getPri1()); //pri1 </script>
时间: 2024-10-09 15:16:43