[js高手之路]面向对象+设计模式+继承一步步改造简单的四则运算
到目前为止,我已经写完了面向对象完整的一个系列知识,前面基本属于理论,原理的理解,接下来,我们就用学到的知识来实战下吧.
看看理解原理和理论是否重要?例子从简单到复杂
一、单体(字面量)封装加减乘除
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高手之路]面向对象+设计模式+继承一步步改造简单的四则运算的更多相关文章
- [js高手之路]面向对象版本匀速运动框架
这篇文章的效果,需要看过以下3篇文章: [js插件开发教程]一步步开发一个可以定制配置的隔行变色小插件 [js高手之路]匀速运动与实例实战(侧边栏,淡入淡出) [js高手之路]打造通用的匀速运动框架 ...
- [js高手之路]寄生组合式继承的优势
在之前javascript面向对象系列的文章里面,我们已经探讨了组合继承和寄生继承,回顾下组合继承: function Person( uName ){ this.skills = [ 'php', ...
- [js高手之路]原型式继承与寄生式继承
一.原型式继承本质其实就是个浅拷贝,以一个对象为模板复制出新的对象 function object( o ){ var G = function(){}; G.prototype = o; retur ...
- [js高手之路] javascript面向对象写法与应用
一.什么是对象? 对象是n个属性和方法组成的集合,如js内置的document, Date, Regexp, Math等等 document就是有很多的属性和方法, 如:getElementById, ...
- [js高手之路]设计模式系列课程-发布者,订阅者重构购物车
发布者订阅者模式,是一种很常见的模式,比如: 一.买卖房子 生活中的买房,卖房,中介就构成了一个发布订阅者模式,买房的人,一般需要的是房源,价格,使用面积等信息,他充当了订阅者的角色 中介拿到卖主的房 ...
- [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表
所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...
- [js高手之路]从原型链开始图解继承到组合继承的产生
基于javascript原型链的层层递进查找规则,以及原型对象(prototype)的共享特性,实现继承是非常简单的事情 一.把父类的实例对象赋给子类的原型对象(prototype),可以实现继承 f ...
- [js高手之路] 设计模式系列课程 - jQuery的extend插件机制
这里在之前的文章[js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数基础上增加一个extend浅拷贝,可以为对象方便的扩展属性和方法, jquery的插件扩展机制,大致就是这 ...
- [js高手之路]Node.js+jade抓取博客所有文章生成静态html文件
这个周末,恶补了一下jade模板引擎,就为生成静态html文件,这篇文章需要知道jade以及看过我的上篇文章,我先给出他们的参考链接: [js高手之路]Node.js模板引擎教程-jade速学与实战1 ...
随机推荐
- Broker模块划分
本篇在上一篇<消息中间件架构讨论>的基础上分析Broker的模块划分. 上图是之前讨论确定的系统架构(后续内容会按照这个架构来叙述),几点基础: Broker采用主从结构 Broker负责 ...
- 智联卓聘 卓聘IM(聊聊)开发实践
1. 卓聘IM开发背景 智联卓聘是智联旗下高端人才招聘平台,成立快4年多,业务增涨每年以100%速度增涨快,同时对产品和研发速度都比较高. 2015年提出IM开发,主要用于后选人与猎头及时交流,降低 ...
- ES6 新增命令
let 用来声明变量.它的用法类似于var,但是所声明的变量, 只在let命令所在的代码块内有效. 例: {var a=10; let b=20;}; console.lo ...
- Centos 7部署大众点评CAT(一)——单服务器部署
前一篇拙作上传的时间已经过去2个月了,中间并不是闲着...主要是忙着学习各种组件的安装,写了几篇安装心得存在硬盘里. 最近尝试了点评开源的CAT监控平台的安装,并且希望能够引入到工作中.在部署实践的过 ...
- FTP服务器 Serv-u 环境搭建
一.安装 *Windows 10 *Serv-u Windows-v15.1.2 *Mysql Mysql 5.7 安装成功后开始配置serv-u. 二.配置 1.新建域(test) 点击新建域,开始 ...
- Entity Framework Core 批处理语句
在Entity Framework Core (EF Core)有许多新的功能,最令人期待的功能之一就是批处理语句.那么批处理语句是什么呢?批处理语句意味着它不会为每个插入/更新/删除语句发送单独的请 ...
- spring入门之环境搭建
本人刚刚接触spring,看了一些教程,但是很多概念都不懂(以前没接触过,看着很抽象),所以我觉得通过动手可能会更好的理解并且掌握.看了一些小实例,但是都没有成功,终于在各种尝试之后搭建成功了,现在我 ...
- ABP从入门到精通(3):aspnet-zero-core 使用Redis缓存
一.Redis是什么? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset ...
- nessus重置密码
许久不用的nessus密码居然忘记了,查了下: cmd下进入到nessus的安装目录 提升为管理员,登录系统 如果想用之前的账号,可以直接在系统内重置密码.
- C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码
前言 什么是Xamarin? Xamarin始创于2011年,旨在使移动开发变得难以置信地迅捷和简单. Xamarin的产品简化了针对多种平台的应用开发,包括iOS.Android.Windows P ...