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

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

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

             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. 使用 QDockWidget嵌套布局来实现复杂界面,方便用户可以自定义界面,自由组合窗口

    http://www.cnblogs.com/findumars/p/5436533.html

  2. thinkphp分页带数据

    因为用thinkphp做分页时候点击下一页后搜索栏的数据会清空,然后点击下一页后刷新完了就没有内容了,感觉网上查找和我自己研究在不适用ajax做分页的情况下用以下代码就可以实现!!通过把值扔地址栏来进 ...

  3. 事件冒泡、事件委托、jQuery元素节点操作、滚轮事件与函数节流

    一.事件冒泡定义 事件冒泡是指在一个对象触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那 ...

  4. zTree学习实例

    今天做完一个zTree的实例,供有需要的学习! 效果图如下:

  5. 构建高并发&高可用&安全的IT系统-高并发部分

    什么是高并发? 狭义来讲就是你的网站/软件同一时间能承受的用户数量有多少 相关指标有 并发数:对网站/软件同时发起的请求数,一般也可代表实际的用户 每秒响应时间:常指一次请求到系统正确响的时间(以秒为 ...

  6. 【HTML】table元素

    1.最简单的table <table> <tr> <th></th> </tr> <tr> <td></td& ...

  7. 共享---samba

    1.  虚拟机,可以采用共享文件夹 2.  windows之间可以使用网络邻居共享 3.  windows与linux,linux与linux之间建立samba服务器 4.  安装samba服务器 r ...

  8. Redis数据类型之ZSet(五)

    前言:有序集合zset跟其他类型一样,同样有几种编码方式.主要有两种编码方式,REDIS_ENCODING_ZIPLIST和REDIS_ENCODING_SKIPLIST.ziplist可以表示较小的 ...

  9. docker在Centos上的安装

    Centos6安装docker 系统:centos6.5 内核:3.10.107-1(已升级),docker对RHEL/Centos的最低内核支持是2.6.32-431,epel源的docker版本推 ...

  10. 创建 overlay 网络 - 每天5分钟玩转 Docker 容器技术(50)

    上一节我们搭建好实验环境,配置并运行了consul,今天开始创建 overlay 网络. 在 host1 中创建 overlay 网络 ov_net1: -d overlay 指定 driver 为 ...