JavaScript 面向对象编程

来源:互联网 发布:淘宝男装女装一起卖 编辑:程序博客网 时间:2024/06/02 05:57

1、模拟重载

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

function Person(){var args=arguments;if(typeof args[0]==='object'&&args[0]){//判断参数是否是对象if(args[0].name){this.name=args[0].name;}if(args[0].age){this.age=args[0].age;}}else{//参数不是对象if(args[0]){this.name=args[0];}if(args[1]){this.age=args[1];}}}Person.prototype.toString=function(){return 'name='+this.name+',age='+this.age;};var zou=new Person('zou',24);alert(zou.toString());//name=zou,age=24var miao=new Person({name:'miao',age:25})alert(miao.toString());//name=miao,age=25


2、调用子类的方法

function Person(name){this.anme=name;}function Student(name,className){this.className=className;Person.call(this,name);//Student继承Person类}var zou=new Student('zou','class319');zou;//Student{className:'class319',name:'zou'}Person.prototype.init=function(){};Student.prototype.init=function(){//do sthperson.prototype.init.apply(this,arguments);}


3、链式调用

function ClassManager(){}ClassManager.prototype.addClass=function(str){alert('Class:'+str+' added');return this;//this指向ClassManager的实例};var manager=new ClassManager();manager.addClass('Math')       .addClass('English')       .addClass('Subject');//Class:Math added//Class:English added//Class:Subject added

4、模拟抽象类

function DetectorBase(){throw new Error('Abstract class can not be invoked directly!');}DetectorBase.detect=function(){console.log('Detection starting...')};DetectorBase.stop=function(){console.log('Detection stropped.');}DetectorBase.init=function(){throw new Error('Error');}//想让子类型覆盖,但是又不想子类型直接调用,可以抛出异常function LinkDetector(){}LinkDetector.prototype=Object.create(DetectorBase.prototype);LinkDetector.prototype.constructor=LinkDetector;

5、defineProperty()

function Person(name){Object.defineProperty(this,'name',{value:name,enumerable:true});}Object.defineProperty(Person,'ARMS_NUM',{value:2,enumerable:true});// {writable: true, enumerable: true, configurable: false} Object.seal(Person.prototype);Object.seal(Person);function Student(name,className){this.className=className;Person.call(this,name);}Student.prototype=Object.create(Person.prototype);Student.prototype.constructor=Student;

6、模块化

var moduleA//prop和func不会被泄漏到全局作用域moduleA=function(){var prop=1;function func(){}return{func:func,prop:prop}}();//立即执行var moduleBmoduleB=new function(){var prop=1;function func(){}this.func=func,this.prop=prop};

7、实例

//立即执行的函数表达式,把内部的函数、变量等包裹住,可以防止变量、函数声明或函数参数泄漏到外部(function(global) {//global==this,浏览器就是window,nodejs就是Globalfunction DetectorBase(configs) { //基类if (!this instanceof DetectorBase) {//防止外部不通过new而是直接调用函数throw new Error('Do not invoke without new.');//直接调用抛出错误} this.configs = configs;this.analyze();//解析数据} //抽象方法,应该被继承,直接调用抛出错误DetectorBase.prototype.detect = function() {throw new Error('Not implemented'); }; DetectorBase.prototype.analyze = function() { console.log('analyze...'); this.data = "###data###"; }; //以下是两个实际的子类function LinkDetector(links) {DetectorBase.apply(this, arguments); if (!this instanceof LinkDetector) { throw new Error('Do not invoke without new.');} this.links = links;}function ContainerDetector(containers) {DetectorBase.apply(this, arguments); if (!this instanceof ContainerDetector) { throw new Error('Do not invoke without new.'); } this.containers = containers; } //实现原型链的继承inherit(LinkDetector, DetectorBase); inherit(ContainerDetector, DetectorBase);// expand later LinkDetector.prototype.detect = function() { console.log('Loading data:' + this.data); console.log('link detection started.'); console.log('Scaning links:' + this.links); }; ContainerDetector.prototype.detect = function() { console.log('Loading data:' + this.data);console.log('link detection started.');console.log('Scaning containers:' + this.containers); }; // prevent from being altered //{writable: false, enumerable: true, configurable: false}  Object.freeze(DetectorBase); Object.freeze(DetectorBase.prototype); Object.freeze(LinkDetector); Object.freeze(LinkDetector.prototype); Object.freeze(ContainerDetector); Object.freeze(ContainerDetector.prototype);// export to global object Object.defineProperties(global, { LinkDetector: { value: LinkDetector },ContainerDetector: { value: ContainerDetector }, DetectorBase: { value: DetectorBase } }); //寄生组合式继承function inherit(subClass, superClass) {subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; } }(this)); var zm = new ContainerDetector('#span #div #input'); zm.detect(); var zz = new LinkDetector('http://blog.csdn.net/qq_27626333'); zz.detect();





0 0
原创粉丝点击