JavaScript 面向对象编程

来源:互联网 发布:淘宝分销骗局 编辑:程序博客网 时间:2024/05/22 20:25

1、模拟重载

  在Java中重载是在一个类里面,方法名字相同,而参数不同。返回类型可以相同也可以不同。每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。由于JavaScript是弱类型,所以没有直接的机制去实现参数重载,但是我们可以通过传入的参数个数来进行模拟的重载。

[javascript] view plain copy
  1. function Person(){  
  2.     var args=arguments;  
  3.     if(typeof args[0]==='object'&&args[0]){//判断参数是否是对象  
  4.         if(args[0].name){  
  5.             this.name=args[0].name;  
  6.         }  
  7.         if(args[0].age){  
  8.             this.age=args[0].age;  
  9.         }  
  10.     }else{//参数不是对象  
  11.         if(args[0]){  
  12.             this.name=args[0];  
  13.         }  
  14.         if(args[1]){  
  15.             this.age=args[1];  
  16.         }  
  17.     }  
  18. }  
  19. Person.prototype.toString=function(){  
  20.     return 'name='+this.name+',age='+this.age;  
  21. };  
  22. var zou=new Person('zou',24);  
  23. alert(zou.toString());//name=zou,age=24  
  24. var miao=new Person({name:'miao',age:25})  
  25. alert(miao.toString());//name=miao,age=25  


2、调用子类的方法

[javascript] view plain copy
  1. function Person(name){this.anme=name;}  
  2. function Student(name,className){  
  3.     this.className=className;  
  4.     Person.call(this,name);//Student继承Person类  
  5. }  
  6. var zou=new Student('zou','class319');  
  7. zou;//Student{className:'class319',name:'zou'}  
  8. Person.prototype.init=function(){};  
  9. Student.prototype.init=function(){  
  10.     //do sth  
  11.     person.prototype.init.apply(this,arguments);  
  12. }  


3、链式调用

[javascript] view plain copy
  1. function ClassManager(){}  
  2. ClassManager.prototype.addClass=function(str){  
  3.     alert('Class:'+str+' added');  
  4.     return this;//this指向ClassManager的实例  
  5. };  
  6. var manager=new ClassManager();  
  7. manager.addClass('Math')  
  8.        .addClass('English')  
  9.        .addClass('Subject');  
  10. //Class:Math added  
  11. //Class:English added  
  12. //Class:Subject added  

4、模拟抽象类

[javascript] view plain copy
  1. function DetectorBase(){  
  2.     throw new Error('Abstract class can not be invoked directly!');   
  3. }  
  4. DetectorBase.detect=function(){console.log('Detection starting...')};  
  5. DetectorBase.stop=function(){console.log('Detection stropped.');}  
  6. DetectorBase.init=function(){throw new Error('Error');}//想让子类型覆盖,但是又不想子类型直接调用,可以抛出异常  
  7. function LinkDetector(){}  
  8. LinkDetector.prototype=Object.create(DetectorBase.prototype);  
  9. LinkDetector.prototype.constructor=LinkDetector;  

5、defineProperty()

[javascript] view plain copy
  1. function Person(name){  
  2.     Object.defineProperty(this,'name',{  
  3.         value:name,  
  4.         enumerable:true  
  5.     });  
  6. }  
  7. Object.defineProperty(Person,'ARMS_NUM',{  
  8.     value:2,  
  9.     enumerable:true  
  10. });  
  11. // {writable: true, enumerable: true, configurable: false}   
  12. Object.seal(Person.prototype);  
  13. Object.seal(Person);  
  14. function Student(name,className){  
  15.     this.className=className;  
  16.     Person.call(this,name);  
  17. }  
  18. Student.prototype=Object.create(Person.prototype);  
  19. Student.prototype.constructor=Student;  

6、模块化
[javascript] view plain copy
  1. var moduleA  
  2. //prop和func不会被泄漏到全局作用域  
  3. moduleA=function(){  
  4.     var prop=1;  
  5.     function func(){}  
  6.     return{  
  7.         func:func,  
  8.         prop:prop  
  9.     }  
  10. }();//立即执行  
  11. var moduleB  
  12. moduleB=new function(){  
  13.     var prop=1;  
  14.     function func(){}  
  15.     this.func=func,  
  16.     this.prop=prop  
  17. };  

7、实例

[javascript] view plain copy
  1. //立即执行的函数表达式,把内部的函数、变量等包裹住,可以防止变量、函数声明或函数参数泄漏到外部  
  2. (function(global) {//global==this,浏览器就是window,nodejs就是Global  
  3.     function DetectorBase(configs) { //基类  
  4.         if (!this instanceof DetectorBase) {//防止外部不通过new而是直接调用函数  
  5.             throw new Error('Do not invoke without new.');//直接调用抛出错误  
  6.         }   
  7.         this.configs = configs;  
  8.         this.analyze();//解析数据  
  9.     }   
  10.     //抽象方法,应该被继承,直接调用抛出错误  
  11.     DetectorBase.prototype.detect = function() {  
  12.         throw new Error('Not implemented');   
  13.     };   
  14.     DetectorBase.prototype.analyze = function() {   
  15.         console.log('analyze...');   
  16.         this.data = "###data###";   
  17.     };   
  18.     //以下是两个实际的子类  
  19.     function LinkDetector(links) {  
  20.         DetectorBase.apply(this, arguments);   
  21.         if (!this instanceof LinkDetector) {   
  22.             throw new Error('Do not invoke without new.');  
  23.         }   
  24.         this.links = links;  
  25.     }  
  26.     function ContainerDetector(containers) {  
  27.         DetectorBase.apply(this, arguments);   
  28.         if (!this instanceof ContainerDetector) {   
  29.             throw new Error('Do not invoke without new.');   
  30.         }   
  31.         this.containers = containers;   
  32.     }   
  33.     //实现原型链的继承  
  34.     inherit(LinkDetector, DetectorBase);   
  35.     inherit(ContainerDetector, DetectorBase);  
  36.     // expand later   
  37.     LinkDetector.prototype.detect = function() {   
  38.         console.log('Loading data:' + this.data);   
  39.         console.log('link detection started.');   
  40.         console.log('Scaning links:' + this.links);   
  41.     };   
  42.     ContainerDetector.prototype.detect = function() {   
  43.         console.log('Loading data:' + this.data);  
  44.         console.log('link detection started.');  
  45.         console.log('Scaning containers:' + this.containers);   
  46.     };   
  47.     // prevent from being altered   
  48.     //{writable: false, enumerable: true, configurable: false}    
  49.     Object.freeze(DetectorBase);   
  50.     Object.freeze(DetectorBase.prototype);   
  51.     Object.freeze(LinkDetector);   
  52.     Object.freeze(LinkDetector.prototype);   
  53.     Object.freeze(ContainerDetector);   
  54.     Object.freeze(ContainerDetector.prototype);  
  55.     // export to global object   
  56.     Object.defineProperties(global, {   
  57.         LinkDetector: { value: LinkDetector },  
  58.         ContainerDetector: { value: ContainerDetector },   
  59.         DetectorBase: { value: DetectorBase }   
  60.     });   
  61.     //寄生组合式继承  
  62.     function inherit(subClass, superClass) {  
  63.         subClass.prototype = Object.create(superClass.prototype);   
  64.         subClass.prototype.constructor = subClass;   
  65.     }   
  66. }(this));   
  67. var zm = new ContainerDetector('#span #div #input');   
  68. zm.detect();   
  69. var zz = new LinkDetector('http://blog.csdn.net/qq_27626333');   
  70. zz.detect();  


原创粉丝点击