[Object]继承(经典版)(五)封装

作者:zccst

封装已经上升到写插件的水平了,与多重继承属于同一个高度,来共同完成实际工作中的挑战。

1,封装
Js代码  收藏代码
  1. var Person = (function(){  
  2.     //定义私有方法,相当于private方法,只能在内部访问  
  3.     function hello(){  
  4.         alert('hello world!');  
  5.     }  
  6.     return function(){  
  7.         //定义私有属性,相当于private属性,只能在公有方法内部访问  
  8.         var name,age;  
  9.         //定义公有方法,相当于public方法,可以在类的实例中方法  
  10.         this.getName = function(){  
  11.             return name;  
  12.         };  
  13.         this.setName = function(newName){  
  14.             name = newName;  
  15.         };  
  16.         this.getAge = function(){  
  17.             return age;  
  18.         }  
  19.         this.setAge = function(newAge){  
  20.             age = newAge;  
  21.         };  
  22.         this.say = hello;  
  23.         this.introduce = function(){  
  24.             alert('my name is :'+this.getName());  
  25.         }  
  26.     }  
  27. })();  
  28.   
  29. var p = new Person();  
  30. p.say(); //hello world  
  31. p.setName('xiaoming');  
  32. p.introduce(); //my name is : xiaoming  
  33.   
  34. //批注:Person本质上是return的那个函数,里面定义了一堆的get和set方法,然后还有私有变量和私有方法。  
  35. //其实私有方法,按照下面的定义方式也一样  
  36. function Person(){  
  37.     var name, age;//私有变量  
  38.     var method1 = function(){};//私有方法  
  39.     var method2 = function(){};  
  40.   
  41.     this.name = name;//实例变量  
  42.     this.sayName = function(){//实例方法  
  43.         alert(this.name);  
  44.     }  
  45. }  

//另外:共有变量跟Person构造函数没关系,静态变量跟继承没关系

在JavaScript模式中,也叫即时函数,即定义完立即执行,并返回一个函数。
再慢慢补充细节吧



2,封装+继承
Js代码  收藏代码
  1. var Person = (function(){  
  2.     //定义私有方法  
  3.     function hello(){  
  4.         console.log('hello world!');  
  5.     }  
  6.     return function(){  
  7.         //定义私有属性,相当于private属性  
  8.         var name,age;  
  9.         //定义公有方法,相当于public方法  
  10.         this.getName = function(){  
  11.             //console.log(name);  
  12.             return name;  
  13.         };  
  14.         this.setName = function(newName){  
  15.             name = newName;  
  16.         };  
  17.         this.getAge = function(){  
  18.             //console.log(age);  
  19.             return age;  
  20.         }  
  21.         this.setAge = function(newAge){  
  22.             age = newAge;  
  23.         };  
  24.         this.say = hello;  
  25.         this.introduce = function(){  
  26.             alert('my name is :'+this.getName());  
  27.         }  
  28.     }  
  29. })();  
  30.   
  31. Person.prototype.protoSay = function(){  
  32.     this.say();//在原型对象的方法里调用实例方法???  
  33. }  
  34.   
  35. var Student = function(){  
  36.     Person.call(this);  
  37. }  
  38. for(var i in Person.prototype){Student.prototype[i] = Person.prototype[i]}  
  39.   
  40. var s = new Student();  
  41. console.log(s);//打印结果如下:  
  42. ////getAge: function (){  
  43. ////getName: function (){  
  44. ////introduce: function (){  
  45. ////say: function hello(){  
  46. ////setAge: function (newAge){  
  47. ////setName: function (newName){  
  48. ////__proto__: Object  
  49. s.setName('xiaoli');  
  50. s.getName();//xiaoli  
  51. //s.protoSay();//helloworld  
  52.   
  53. var p = new Person();  
  54. console.log(p);  
  55. p.protoSay()  


里面牵扯到一个问题:原型方法能调用实例方法?
详见:http://zccst.iteye.com/blog/2077566
原文地址:https://www.cnblogs.com/shsgl/p/4289913.html