<script>
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>
打印如下:
pub1
undefined
pri1