我的JS 类 写法

长这样!

 1 var p,p1;
 2 
 3 
 4 //构造函数
 5 function Person(name) {
 6     this.name = name;
 7 }
 8 //原型对象
 9 var proto = {
10     getName : function(){return this.name},
11     setName : function(name){this.name = name;} ,
12     age : 100
13 }
14  for(var atr in proto) {
15     Person.prototype[atr] = proto[atr];
16 }       
17 //写两个类
18 p = new Person("小王");
19 console.log(p); 
20 p.age = 200;
21 console.log(p.age); 
22 p1 = new Person("小王");
23 console.log(p1.age); 

继承。

 1 //继承。
 2 function Man(name,height,huzi){
 3     Person.call(this,name,height);//this,指向的是Person本身,代表person函数,也可以说是person构造函数
 4     this.huzi = huzi;
 5 }
 6 //Man.prototype = Person;//问题出来了,Man的原型函数只包含person类中的基本属性name和height
 7  for(var atr in proto) {
 8     Man.prototype[atr] = Person.prototype[atr];
 9 }    
10 var m = new Man( "韩寒","173cm","有");
11 console.log(m);
12 console.log(m.age);
原文地址:https://www.cnblogs.com/xunol/p/3216808.html