到目前为止,我已经写完了面向对象完整的一个系列知识,前面基本属于理论,原理的理解,接下来,我们就用学到的知识来实战下吧.

看看理解原理和理论是否重要?例子从简单到复杂

一、单体(字面量)封装加减乘除

             var Oper = {
add : function( n1, n2 ){
return n1 + n2;
},
sbb : function( n1, n2 ){
return n1 - n2;
},
mul : function( n1, n2 ){
return n1 * n2;
},
div : function( n1, n2 ){
return n1 / n2;
},
};
console.log( Oper.add( 10, 20 ) ); //
console.log( Oper.sbb( 10, 20 ) ); //-10
console.log( Oper.mul( 10, 20 ) ); //
console.log( Oper.div( 10, 20 ) ); //0.5

二、构造函数方式

         function Oper( n1, n2 ){
this.num1 = n1 || 0;
this.num2 = n2 || 0;
this.setData = function( n1, n2 ){
this.num1 = n1;
this.num2 = n2;
};
this.add = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 + this.num2;
};
this.sbb = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 - this.num2;
};
this.mul = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 * this.num2;
};
this.div = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 / this.num2;
};
}; console.log( new Oper( 10, 20 ).add() ); //
console.log( new Oper(100, 200).sbb( 10, 20 ) ); //-10
console.log( new Oper().mul( 10, 20 ) ); //
console.log( new Oper().div( 10, 20 ) ); //0.5

三、构造函数+原型对象(prototype)

         function Oper(n1, n2) {
this.num1 = n1 || 0;
this.num2 = n2 || 0;
};
Oper.prototype = {
constructor : Oper,
setData : function (n1, n2) {
this.num1 = n1;
this.num2 = n2;
},
add : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 + this.num2;
},
sbb : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 - this.num2;
},
mul : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 * this.num2;
},
div : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 / this.num2;
}
};
console.log(new Oper().add(10, 20)); //
console.log(new Oper( 100, 200 ).sbb()); //-100
console.log(new Oper().mul(10, 20)); //
console.log(new Oper().div(10, 20)); //0.5

四、寄生组合继承+简单工厂模式

         function Oper( n1, n2 ){
this.num1 = n1;
this.num2 = n2;
};
Oper.prototype.run = function(){}
function object( o ){
var G = function(){};
G.prototype = o;
return new G();
};
function inheritPrototype( subObj, superObj ){
var proObj = object( superObj.prototype );
proObj.constructor = subObj;
subObj.prototype = proObj;
} function OperAdd( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperAdd, Oper );
OperAdd.prototype.run = function(){
return this.num1 + this.num2;
} function OperSbb( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperSbb, Oper );
OperSbb.prototype.run = function(){
return this.num1 - this.num2;
} function OperMul( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperMul, Oper );
OperMul.prototype.run = function(){
return this.num1 * this.num2;
} function OperDiv( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperDiv, Oper );
OperDiv.prototype.run = function(){
return this.num1 / this.num2;
} function OperFactory( oper, n1, n2 ){
switch( oper ) {
case '+':
return new OperAdd( n1, n2 ).run();
break;
case '-':
return new OperSbb( n1, n2 ).run();
break;
case '*':
return new OperMul( n1, n2 ).run();
break;
case '/':
return new OperDiv( n1, n2 ).run();
break;
}
} console.log( OperFactory( '+', 10, 20 ) ); //
console.log( OperFactory( '-', 10, 20 ) ); //-10
console.log( OperFactory( '*', 10, 20 ) ); //
console.log( OperFactory( '/', 10, 20 ) ); //0.5

这种方式,虽然增加了代码量, 如果这道题是实际运用,比如说后面还有很多种运算,两个数的乘方,立方,平方等等,

还有其他特殊处理等等,那么这种扩展性就非常强

[js高手之路]面向对象+设计模式+继承一步步改造简单的四则运算的更多相关文章

  1. [js高手之路]面向对象版本匀速运动框架

    这篇文章的效果,需要看过以下3篇文章: [js插件开发教程]一步步开发一个可以定制配置的隔行变色小插件 [js高手之路]匀速运动与实例实战(侧边栏,淡入淡出) [js高手之路]打造通用的匀速运动框架 ...

  2. [js高手之路]寄生组合式继承的优势

    在之前javascript面向对象系列的文章里面,我们已经探讨了组合继承和寄生继承,回顾下组合继承: function Person( uName ){ this.skills = [ 'php', ...

  3. [js高手之路]原型式继承与寄生式继承

    一.原型式继承本质其实就是个浅拷贝,以一个对象为模板复制出新的对象 function object( o ){ var G = function(){}; G.prototype = o; retur ...

  4. [js高手之路] javascript面向对象写法与应用

    一.什么是对象? 对象是n个属性和方法组成的集合,如js内置的document, Date, Regexp, Math等等 document就是有很多的属性和方法, 如:getElementById, ...

  5. [js高手之路]设计模式系列课程-发布者,订阅者重构购物车

    发布者订阅者模式,是一种很常见的模式,比如: 一.买卖房子 生活中的买房,卖房,中介就构成了一个发布订阅者模式,买房的人,一般需要的是房源,价格,使用面积等信息,他充当了订阅者的角色 中介拿到卖主的房 ...

  6. [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表

    所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...

  7. [js高手之路]从原型链开始图解继承到组合继承的产生

    基于javascript原型链的层层递进查找规则,以及原型对象(prototype)的共享特性,实现继承是非常简单的事情 一.把父类的实例对象赋给子类的原型对象(prototype),可以实现继承 f ...

  8. [js高手之路] 设计模式系列课程 - jQuery的extend插件机制

    这里在之前的文章[js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数基础上增加一个extend浅拷贝,可以为对象方便的扩展属性和方法, jquery的插件扩展机制,大致就是这 ...

  9. [js高手之路]Node.js+jade抓取博客所有文章生成静态html文件

    这个周末,恶补了一下jade模板引擎,就为生成静态html文件,这篇文章需要知道jade以及看过我的上篇文章,我先给出他们的参考链接: [js高手之路]Node.js模板引擎教程-jade速学与实战1 ...

随机推荐

  1. CJOJ 1308 【HNOI 2002 】营业额统计 / CodeVS 1296 营业额统计(STL,二分)

    CJOJ 1308 [HNOI 2002 ]营业额统计 / CodeVS 1296 营业额统计(STL,二分) Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一 ...

  2. IntentService与Service的区别

    IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行 ...

  3. (转)sizeof

    数据类型的大小(即所占字节数)以及能够表示的数据范围是与编译器和硬件平台有关的."float.h"头文件(如vc6.0,在include目录下)通常定义了基本数据类型能够表示的数据 ...

  4. ubuntu下升级网卡驱动

    ubuntu下升级网卡驱动 无线局域网环境下,有个笔记本儿的无线经常断,而其它的终端都好好的,唯独它不行.所以想到检查和更新下无线网卡的驱动看看.以下是操作流程,记录一下. 阅读说明:##为标签, / ...

  5. 初学Python(九)——函数

    初学Python(九)--函数 初学Python,主要整理一些学习到的知识点,这次是函数. 函数定义: # -*- coding:utf-8 -*- #函数的定义 def my_function(x) ...

  6. 人工智能(AI)库TensorFlow 踩坑日记之二

    上次 踩坑日志之一 遗留的问题终于解决了,所以作者(也就是我)终于有脸出来写第二篇了. 首先还是贴上 卷积算法的示例代码地址 :https://github.com/tensorflow/models ...

  7. 小白也能看懂的插件化DroidPlugin原理(三)-- 如何拦截startActivity方法

    前言:在前两篇文章中分别介绍了动态代理.反射机制和Hook机制,如果对这些还不太了解的童鞋建议先去参考一下前两篇文章.经过了前面两篇文章的铺垫,终于可以玩点真刀实弹的了,本篇将会通过 Hook 掉 s ...

  8. 怎么关闭wps热点?永久关闭wps右下角弹窗的方法!

    wps热点总是开机或者开启WPS后在任务栏闪烁,影响心情,百度了一下找到的方法也过时了.我的是WIN10系统 所以自己摸索了一下,找到了解决办法.其实还是用空白文件替换wps热点的.exe文件,只是这 ...

  9. Swift学习之构造方法

    定义 构造过程是为了使用某个类.结构体或枚举类型的实例进行的准备过程.这个过程包含了为实例中的每个属性设置初始值和为其执行必要的准备和初始化任务. 构造方法可以被归结为指定构造方法与遍历构造方法,在S ...

  10. JAVA环境变量关于

    1.为什么要设置classPath? 用于通知JVM Java基础类库的位置.classPath告诉类装载器去哪里寻找第三方类库 自JDK1.5之后便不需要再配置这个变量了 2.为什么安装两个JRE( ...