JavaScript 面向对象编程
来源:互联网 发布:淘宝分销骗局 编辑:程序博客网 时间:2024/05/22 20:25
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=24
- var miao=new Person({name:'miao',age:25})
- alert(miao.toString());//name=miao,age=25
- 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 sth
- person.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 moduleB
- moduleB=new function(){
- var prop=1;
- function func(){}
- this.func=func,
- this.prop=prop
- };
7、实例
- //立即执行的函数表达式,把内部的函数、变量等包裹住,可以防止变量、函数声明或函数参数泄漏到外部
- (function(global) {//global==this,浏览器就是window,nodejs就是Global
- function 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
- JavaScript 面向对象编程
- JavaScript 面向对象编程
- javascript面向对象编程
- javascript 面向对象编程
- JavaScript面向对象编程
- JavaScript 面向对象编程
- JAVASCRIPT - - 面向对象编程
- JavaScript面向对象编程
- JavaScript面向对象编程
- JavaScript面向对象编程
- javascript面向对象编程
- JavaScript面向对象编程
- javascript面向对象编程
- JavaScript面向对象编程
- JavaScript面向对象编程
- JavaScript面向对象编程
- JavaScript面向对象编程
- JavaScript面向对象编程
- flex布局
- 1107: 回文数猜想(函数专题)
- POJ 1258 Agri-Net (最小生成树 + 水题)
- hp-ux系统备份与恢复常用命令
- 容器、线程、IO测试总结FancyLab
- JavaScript 面向对象编程
- API和库函数有什么区别
- T-Finder: A Recommender System for Finding Passengers and Vacant Taxis阅读
- 五. 使用Markdown写幻灯片之Marp
- 修改kali2.0系统默认语言为中文
- 明远智睿i.MX6开发板EK200捡漏之三:添加显示屏驱动支持
- 结合人类视觉注意力
- 欢迎使用CSDN-markdown编辑器
- ZOOM论文