1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| js实现private,public,static和继承 / //构造函数 function person(){ this.name = ‘’;//这个相当于public var age =12;//private //不建议每个函数都用this.getAge,浪费内存, //但是对于私有变量age只能用this.getAge来获取值 this.getAge = function(){ return age; }; } //添加静态属性或方法 person.TYPE = ‘people’; person.sayHello = function(){ console.log(‘hello world’); } //类的public方法 person.prototype.setName=function(name){ this.name = name; } person.prototype.getName=function(){ return this.name; }
person.prototype.setAge = function(age){ this.age = age; }
var p = new person(); p.setName(‘lili’); p.setAge(45);
//静态类 var staticClass = {}
|